Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get id value by index using name jquery

html

<input id="1" name="myText" type="text" value="20"/>
<input id="2" name="myText" type="text" value="30"/>
<input id="3" name="myText" type="text" value="40"/>

How can I get id value by index using name?

The following code snippet is not working

var getVal = $('[name="myText"]').index(1);
like image 746
Shahid Ghafoor Avatar asked Dec 05 '22 16:12

Shahid Ghafoor


1 Answers

jQuery holds the DOM elements in the set like an array so you can use the indexes operator([]) to get the element, or get the jQuery object that wraps the desired element with :eq(n) `.eq(n)`

$('input[name="myText"]:eq(1)').attr('id')

You should mention what to you consider to be index(1) the first or the second:

$('input[name="myText"]:eq(0)').attr('id') // First
$('input[name="myText"]:eq(1)').attr('id') // Second

Or:

$('input[name="myText"]')[0].id // First
like image 172
gdoron is supporting Monica Avatar answered Dec 24 '22 23:12

gdoron is supporting Monica