1

I have the following HTML:

<select id="MySelector" multiple="multiple">
  <option value="2">Option A</option>
  <option value="7">Option B</option>
  <option value="9">Option C</option>
  <option value="12">Option D</option>
</select>

<div id="CheckSelected">click here to select</div>

I'm looking to loop through the options of the select and determine which ones have been selected.

I'm looking to do something like this:

ArrayOfSelected = [];

$('#CheckSelected').click(function {

 "if option is selected then add value to the array"

});

Thanks for your suggestions.

1
  • Is this a new array you're creating or do you have an existing array you want to add values to? Commented Apr 7, 2011 at 19:30

5 Answers 5

6

$('#MySelector').val() gives you an array containing the values of the selected elements.

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

Comments

2

something like this should work for you. Find the option:selected and for each one push the value into the array.

$('#CheckSelected').click(function() {
    ArrayOfSelected.length = 0; //empty the array.
    $("#MySelector option:selected").each(function() {
        ArrayOfSelected.push($(this).val());
    });
});

Code example on jsfiddle.

Comments

1

try this:

ArrayOfSelected = [];

$('#CheckSelected').click(function {

   $('#MySelector option:selected').each(function(){
      ArrayOfSelected.push($(this).val());
   })

});

Comments

1

You can do this with live select option box clicks rather than having to select an option then click the "click here to select" div. Do the following:

  var ArrayOfSelected = [];
  $('select#MySelector').change(function(){
      ArrayOfSelected.length = 0;
      ArrayOfSelected.push($(this).val());
      $("#picks").empty().append("<h1>selected: " + ArrayOfSelected + "</h1>");
 });

The .change() function tells the callback function to execute when an option within the specified select input has been changed. So for every option box click, the callback will be executed...

Doing it this way will reduce the number of clicks the user has to do in order to add an option to the array.

update

I tested this in your jsfiddle link and it works the way you are wanting.

Comments

-2

You can do it without looping with jQuery:

$('#CheckSelected').click(function() {
    if($('#MySelector').val() == 'your_option') {
        //add to array code
    }
}

3 Comments

this code is kind of useless... where does this if statement go?
If this if statement is outside of a loop then what initiates the if to execute? You didn't post enough code to help him out...
My bad, you both are right. You don't drink and code. I'll try to remember that tomorrow :D

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.