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.

starball
60k53 gold badges323 silver badges1k bronze badges
asked Apr 13, 2010 at 7:48

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

answered Apr 13, 2010 at 7:52
Sign up to request clarification or add additional context in comments.

11 Comments

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 - 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.
why do we need .get() for?
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 - $(...) returns an array-like jQuery wrapper. $(...).get() returns a real array of real DOM nodes, not a string.
|
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());
});
answered Apr 13, 2010 at 7:51

1 Comment

@Sarfraz please check it codepen.io/sidaurukfreddy/pen/XNroJW how can i prevent if kode barang has same value ? thanks
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());
});
answered Apr 13, 2010 at 7:53

Comments

10

To catch the names array, i use that:

$("input[name*='task']")
answered Apr 10, 2013 at 10:45

1 Comment

This is actually the best answer here.
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));
});
Emilio Gort
3,4553 gold badges32 silver badges44 bronze badges
answered Apr 13, 2010 at 7:50

2 Comments

@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.
@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.
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());
});
answered Feb 20, 2017 at 21:50

Comments

0

if you want selector get the same id, use:

$("[id=task]:eq(0)").val();
$("[id=task]:eq(1)").val();
etc...
answered Sep 3, 2014 at 2:21

Comments

0

You can use jquery.serializeJSON to do this.

answered Oct 16, 2016 at 21:33

Comments

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()
answered Sep 14, 2019 at 4:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.