72

Here is my html input elements

<input type="text" name="pname[]" value="" />
<input type="text" name="pname[]" value="" />
<input type="text" name="pname[]" value="" />
<input type="text" name="pname[]" value="" />
<input type="text" name="pname[]" value="" />
<input type="text" name="pname[]" value="" />

How can I get all the values of pname array using Jquery

7
  • i cannot understand.. want to get the values of the text box..? Commented Jul 1, 2014 at 6:39
  • Yes I want to get all textbox values in array format Commented Jul 1, 2014 at 6:40
  • use Array mapping feature Commented Jul 1, 2014 at 6:41
  • 1
    Both are not same questions @RajaprabhuAravindasamy read it properly. Commented Jul 1, 2014 at 6:47
  • @user3707303 Yes both are not same, your question contains less words while comparing with the linked question. You got it.. Commented Jul 1, 2014 at 6:50

3 Answers 3

189

By Using map

var values = $("input[name='pname[]']")
              .map(function(){return $(this).val();}).get();
Sign up to request clarification or add additional context in comments.

4 Comments

what does get() do? What does this return without the get()?
get() method returns a specified element from map object. Without .get() you will get entire map object with things like length, context, etc.
Where is get() defined? Is that a jQuery thing? An Array.map thing?
this is how you got values. How to get keys?
36

You can use .map().

Pass each element in the current matched set through a function, producing a new jQuery object containing the return value.

As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array.

Use

var arr = $('input[name="pname[]"]').map(function () {
    return this.value; // $(this).val()
}).get();

1 Comment

this is how you got values. How to get keys?
19

Use:

function getvalues() {
  var inps = document.getElementsByName('pname[]');
  for (var i = 0; i < inps.length; i++) {
      var inp = inps[i];
      alert("pname[" + i + "].value=" + inp.value);
  }
}

Here is Demo.

1 Comment

@Genki, Glad that it helped someone! Cheers! Happy coding :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.