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
Paengski
  • 229
  • 2
  • 6
  • 15

5 Answers5

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/

webdeveloper
  • 16,626
  • 3
  • 46
  • 47
  • More data: [What are valid values for the id attribute in HTML?](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) – webdeveloper Sep 20 '12 at 08:39
1
$("select[name='operator[]']").val();

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

Riz
  • 8,611
  • 7
  • 33
  • 52
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.

Jon
  • 396,160
  • 71
  • 697
  • 768
0

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

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

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

Demo

Clyde Lobo
  • 8,747
  • 6
  • 34
  • 58
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.

Nikola Petkanski
  • 4,320
  • 30
  • 41