x4tje x4tje - 1 year ago 83
jQuery Question

How to get an Array with jQuery, multiple <input> with the same name

I have a form where users can add input fields with jQuery.

<input type="text" id="task" name="task[]" />

After submitting the form I get an array in PHP.

I want to handle this with the
but I have no idea how to turn my
s to an array in jQuery.

Thanks in advance.

Answer Source

Using map:

var values = $("input[id='task']")
              .map(function(){return $(this).val();}).get();

If you change or remove the id (which should be unique), you may also use the selector $("input[name='task\\[\\]']")

Working example: http://jsbin.com/ixeze3

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download