Ajay Takur
Ajay Takur

Reputation: 6236

Assign multiple ids

I have two different forms one for entry and another for update the same.so i did take

<form id="form1" action="action.do">
<input type="text" id="a" value="1">
</form> 

and

<form id="form2" action="action.do">
<input type="text" id="a" value="2">
</form>

I have to access both inputs.I did try like var inputs=$('#form1 #a,#form2 #a').val();

form2 input value is not retrieved.How do i fetch values when form ids are similar but input ids are different with single jquery function ?

Upvotes: 0

Views: 161

Answers (1)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

Although ID should be unique, that is not the problem here, you should still change them for a class. It will also simplify your selector.

When using .val() as a getter, it will not get every value of your stack, but only the value of the first element. If you want to have an array of value, you should use .map().

var inputs=$('#form1 #a,#form2 #a').map(function(){
    return this.value;
}).get();

You need to end with .get() since .map() return a jQuery object. .get() will change it for a native JS array.

Upvotes: 1

Related Questions