2

I have form with inputs

<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]">

How I can receive data from 3rd input, for example?

This construction doesn't work

$('input[name="name[2]"]').val();
3
  • $('input:text:nth-child(3)').val(); Commented Dec 22, 2016 at 9:18
  • $('input:text:eq(2)').val(); Commented Dec 22, 2016 at 9:19
  • @2oppin i doubt it will work Commented Dec 22, 2016 at 9:20

2 Answers 2

1

You can use :eq() selector or .eq() method

alert($('input:text:eq(2)').val());
alert($('input:text').eq(2).val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]" value="1">
<input type="text" name="name[]">
<input type="text" name="name[]">

Sign up to request clarification or add additional context in comments.

Comments

0

If you are the one who writes the inputs, the best practice here is to use ids

<input id="input1" type="text" name="name[]">
<input id="input2" type="text" name="name[]">
<input id="input3" type="text" name="name[]">
<input id="input4" type="text" name="name[]">
<input id="input5" type="text" name="name[]">

To recover de data of the input:

var text = $('#input3').val();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.