Reputation: 41
i have a text boxes with same name and id, i want to get value of a particular text box and validate, can some one help how to achieve this as i am new to this.
text box name, id are same but values are different.
Upvotes: 1
Views: 5062
Reputation: 41
Thanks for the help, I gave text boxes different name using id="{concat('txtboxAdjustment', position())}" inside loop and it was done but i got good idea about the topic reading all the comments so thanks again.
Upvotes: 0
Reputation:
same name is enough to do the trick!
alert($("input[name='txtboxName']").eq(0).val());
alert($("input[name='txtboxName']").eq(1).val());
Upvotes: 0
Reputation: 4259
Not that you ever should have two or more of the same id's, but if you do, here's how to deal with it:
jQuery:
var inputs = $('input#my_id')
, val1 = inputs.eq(0).val()
, val2 = inputs.eq(1).val()
;
Plain JavaScript
var inputs = document.getElementsByTagName('input');
var input, val1, val2;
foreach ( var i in inputs )
{
// This code may not work, but you get the idea
input = inputs[i];
if (input.id == 'test')
{
if ( !val1 ) val1 = input.value;
else if ( !val2 ) val2 = input.value;
else break;
}
}
Upvotes: 3
Reputation: 21690
$("#test").val();
it selects only the first element with the given ID. you could further refine your selection by specifying the tag
<input type=text class='fo' id='test' value="1">
<input type=text class='fp' id='test' value="2">
$("[id=test][class=fp]").val();
http://jsfiddle.net/suhailvs/KY9Tu/
Upvotes: 0
Reputation: 20840
There should not be multiple elements with same Id. You can assign same class to both element and then access it.
document.getElementsByClassName('test')[0] //1
document.getElementsByClassName('test')[1] //2
Upvotes: 3
Reputation: 8393
As has already been pointed out you shouldn't have two elements with the same identifier.
<input type="text" id="test" name="test" value="1">
<input type="text" id="test" name="test" value="2">
However, if you wanted to selected something by its value (generally a bad idea), you could so so with:
$(':input[value="2"]')
A better approach would be to have two unique text boxes like so:
<input type="text" id="test1" name="test" value="1">
<input type="text" id="test2" name="test" value="2">
Upvotes: 3
Reputation: 178
<input type=text id='test' value="1">
<input type=text id='test' value="2">
document.getElementsByTagName('input')[0].value //1
document.getElementsByTagName('input')[1].value //2
very dirty way of doing things
Upvotes: 3