Subhojit Mukherjee
Subhojit Mukherjee

Reputation: 719

JS get element id array

My html code is

<select id="child[1]" name="child[1]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="child[2]" name="child[2]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

My JS code not working

JS code is

alert(jQuery("#child").length);

Can you please help me how can I get the child array, I want to send the array through AJAX

Upvotes: 0

Views: 1054

Answers (2)

Joseph
Joseph

Reputation: 119837

jQuery("select[name^=child]").serializeArray()

what this does is:

  1. get select elements that have a name that starts with "child"
  2. serializes them into an array

the output is something like:

[
    {name:'child[1]', value:'1'},
    {name:'child[2]', value:'1'}
]

http://jsfiddle.net/qdK7V/1/

Upvotes: 0

silly
silly

Reputation: 7887

change your jquery selector to

jQuery("[name*=child\\[]")

Upvotes: 1

Related Questions