A
A
Anastasia2021-06-18 16:22:38
JavaScript
Anastasia, 2021-06-18 16:22:38

How to pass value from site link to Select?

Hello everyone
. I have this site link: mysite.com/about/en

I also have a select with values:

<select class="select__language">
        <option value="en">English</option>
        <option value="pl">Polish</option>
        <option value="fr">French</option>
</select>


And I need, when loading the page, depending on the link, add the selected value to the desired option in option.

That is, if the site link ends with fr , then I want to add selected to French .
To create a select, I use the library https://jqueryniceselect.hernansartorio.com/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2021-06-18
@ThunderCat

get the xref line, split it by /, take the last element - voila, got your fr or whatever...
getElementBy Classname( 'select__language' ).options - got a list of options.
We sort through, compare the value, give the required attribute selected.
Profit.

A
Alexey, 2021-06-18
@Azperin

document.querySelector('.select__language').value = window.location.pathname.split('/').pop();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question