Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector by form index and input name

Take the following page with two forms with different classes but each form has an input with the same name.

<form class='first_form'>
 <input name='test' value='1' />
</form>
<form class='second_form'>
 <input name='test' value='3'/>
</form>

I can get the form index and I know the name of the input but I do not know the index of the input.

Is there a way to chain a selector with the form index and the input name to get the value?

I have tried chaining but nothing seems to work

var inputName = 'test';
Var formIndex = 1;

$('*[name="' + inputName + '"]' +' ' + '$("form").eq(' + formIndex + ')').val();
like image 359
Bob Knothe Avatar asked Dec 20 '22 10:12

Bob Knothe


2 Answers

FIDDLE

var formIndex=0;
var inputName="txtbox";
vall= $("form:eq("+ formIndex+") input[name= "+ inputName +" ]").val();
alert(vall);

your order was wrong

like image 81
Pratik Avatar answered Jan 09 '23 22:01

Pratik


Untested, but could you do:

$('form:nth-of-type(1) input[name="test"]').val();
like image 35
Dan Blows Avatar answered Jan 09 '23 22:01

Dan Blows