0
<select size="6" name="operator[]" id="operator[]" size="5" multiple="">
    <option value="1" >One</option>
    <option value="2" >Two</option>
    ....
    ....
    <option value="10" >Ten</option>
</select>

My question now is how can I access the array values of dropdownbox using jquery?

$("#operator").val(); // Not working
$("#operator[]").val(); // Not working as well
1
  • Anyway to avoid using [] in ID ??? Commented Sep 20, 2012 at 8:27

5 Answers 5

1

This is not valid id and name property, use this code:

$("#operator").find('option:selected').each(function(){
    alert(this.value);
});

demo: jsfiddle.net/VYjEM/

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

1 Comment

1
$("select[name='operator[]']").val();

Example: http://jsbin.com/eqoyes/1/edit

Comments

0

The syntax $("select").val() will give you the values of the selected options. If you want to get hold of all the options, use $("select > option").

Also: using the characters [] in the id attribute is illegal in HTML 4 and it does not buy you anything particular; you should probably fix that.

Comments

0

First things first. I think it is not allowed to use an array for element id. With that being said, set the element id to 'operator' rather than 'operator[]'.

After you've done that, you can get the options in several ways:

  1. Using the element dom id:

    $('#operator option');
    
  2. Using the element dom type:

    $('select option');
    
  3. Using both (recommended):

    $('select#operator option');
    

More information about the selectors jQuery supports can be found at the official website.

Comments

0

You will have to escape the [ and ] using \\

$("#operator\\[\\]").val();

or you can use $("select[id='operator[]']").val()

Demo

Comments

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.