Get all selected values from <select> with Javascript - Duplicate question

  1. Answer:


To get all the selected values from a <select> element with JavaScript, you can use the following code:

var selectElement = document.getElementById("mySelect");
var selectedValues = [];

for (var i = 0; i < selectElement.options.length; i++) {
  if (selectElement.options[i].selected) {

In this code, we first get the <select> element using its ID. Then, we create an empty array to store the selected values. Next, we loop through all the options in the select element and check if each option is selected. If an option is selected, we add its value to the selectedValues array.

Finally, we have an array with all the selected values from the <select> element.

This code should work for any <select> element on your page, as long as you replace "mySelect" with the ID of your select element.

I hope this helps!

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information