65

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 $.ajax() but I have no idea how to turn my <input>s to an array in jQuery.

9 Answers 9

124

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

11
  • this is working good with the 1,2,3 so i'll use this with the explode function of php to get it sorted thanks @all for helping
    – x4tje
    Commented Apr 13, 2010 at 8:10
  • 1
    @x4tje - no problem. You don't have to use explode - note that values is an array, not a string. It depends on how you do your AJAX call, or course.
    – Kobi
    Commented Apr 13, 2010 at 8:12
  • why do we need .get() for?
    – imdahmd
    Commented Mar 27, 2013 at 8:55
  • 1
    The .get() flattens the array of values into a string. If you did want the actual Javascript array of values, you should remove the ".get()".
    – orrd
    Commented Apr 30, 2013 at 22:46
  • 1
    @orrd - $(...) returns an array-like jQuery wrapper. $(...).get() returns a real array of real DOM nodes, not a string.
    – Kobi
    Commented May 1, 2013 at 4:55
35

For multiple elements, you should give it a class rather than id eg:

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

Now you can get those using jquery something like this:

$('.task').each(function(){
   alert($(this).val());
});
1
17

Firstly, you shouldn't have multiple elements with the same ID on a page - ID should be unique.

You could just remove the id attribute and and replace it with:

<input type='text' name='task'>

and to get an array of the values of task do

var taskArray = new Array();
$("input[name=task]").each(function() {
   taskArray.push($(this).val());
});
10

To catch the names array, i use that:

$("input[name*='task']")
1
  • This is actually the best answer here.
    – tmarois
    Commented Jul 12, 2014 at 1:25
9

You can't use same id for multiple elements in a document. Keep the ids different and name same for the elements.

<input type="text" id="task1" name="task" />
<input type="text" id="task2" name="task" />
<input type="text" id="task3" name="task" />
<input type="text" id="task4" name="task" />
<input type="text" id="task5" name="task" />

var newArray = new Array();

$("input:text[name=task]").each(function(){
    newArray.push($(this));
});
2
  • 2
    @rahul: that won't do because he is dealing with multiple text boxes not one. If you know PHP, notice the name of the text box task[] which means there are supposed to be more than one text boxes.
    – Sarfraz
    Commented Apr 13, 2010 at 7:52
  • 2
    @rahul: your update won't help still in terms of making php array which can be done if the [] is suffixed with the name of the field.
    – Sarfraz
    Commented Apr 13, 2010 at 7:56
1

HTML:

<input type="text" name="task[]" class="form-control" id="task">

JS:

var tasks= new Array();
$('input[name^="task"]').each(function() 
{
tasks.push($(this).val());
});
0

if you want selector get the same id, use:

$("[id=task]:eq(0)").val();
$("[id=task]:eq(1)").val();
etc...
0
0

You can use jquery.serializeJSON to do this.

0

Q:How to access name array text field

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

Answer - Using Input name array :

$('input[name="task\\[\\]"]').eq(0).val()
$('input[name="task\\[\\]"]').eq(index).val()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.