2

i have a select element with multiple selection, like this:

<select multiple>
  <option value="en">English</option>
  <option value="de">German</option>
</select>

And i have a javascript, which should use selected values, looking like:

$.ajax({
                url: window.location.protocol + "//example.com/search",
                jsonp: "jsonp",
                dataType: "jsonp",
                data: {
                q: querykeyword,
                client: "chrome",
                hl: "HERE SHOULD APPEAR ONE OR TWO SELECTED VALUES"
                }

So the last code line should look like hl: "en" or, hl: "de", or hl: ["en","de"].

How can i transfer values from select element into javascript?

3
  • 1
    document.querySelector("select").value Commented Dec 30, 2020 at 19:11
  • It would make your backend less complicated if you sent hl: ["en"] and consistently sent an array Commented Dec 30, 2020 at 19:13
  • 1
    Does this answer your question? How to get all selected values from <select multiple=multiple>? Commented Dec 30, 2020 at 19:20

2 Answers 2

2

Use selectedOptions property.

var selectedOptions = document.querySelector("select").selectedOptions;
var values = [];
for (let i=0; i<selectedOptions.length; i++) {
  values.push(selectedOptions[i].value);
}

.value will not work, as it will show only one selected option.

console.log(document.querySelector("select").value)
<select multiple>
  <option value="volvo" selected>Volvo</option>
  <option value="saab" selected>Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

Sign up to request clarification or add additional context in comments.

9 Comments

You just need to get the .value of the select. Also, instead of deleting an answer and then posting a new one, you can just edit your deleted answer and then undelete it.
.value() will return the only one value when you select multiple options.
as per OP, there is no dropdown mention, w3schools.com/tags/att_select_multiple.asp
This question has been asked and answered many times before. There is no reason to answer it again.
@OrAssayag In the OP: <select multiple>, which means the user can select multiple options.
|
0

You need to take the selected value from the select element. You can do that by: document.querySelector("select").value, so the full solution for your code will be:

$.ajax({
      url: window.location.protocol + "//example.com/search",
      jsonp: "jsonp",
      dataType: "jsonp",
      data: {
        q: querykeyword,
        client: "chrome",
        hl: document.querySelector("select").value
      });

11 Comments

A dump of code is rarely a great answer. Code, properly formatted, along with an explanation of the code and why/how it works is much more often upvoted (on questions which are not common duplicates).
.value will show only one selected option. refer to my answer about the example.
Or, refer to the answers to the duplicate question.
no it's correct. according to his question description, he need hl: ["en","de"]
yes, you are right :( its only one value here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.