Reputation: 624
I need a value of text box
for that I am using document.getElementsByName("elemntName")
but the problem is the the name
itself is dynamic, something like below.
for(temp = 0 ; temp < arracid.length ; temp++){
cell1=mynewrow.insertCell(4);
cell1.innerHTML="<input type='hidden' name='ACID"+index+"' class='tblrows' id='ACID"+index+"' value='"+arracid[temp]+"'>";
index++;
}
When I tried var acidCount = document.getElementsByName('ACID')
its not working and I tried
var acidCount = document.getElementsByName('ACID"+index+"')
still not working
For every loop the name is changing like ACID1
,ACID2
etc.. can anyone help me how to get the value
of this text box?
Upvotes: 1
Views: 1030
Reputation: 1616
Since you are naming your elements 'ACID' + index
, you can utilize the querySelector
method as follows:
for (var i=0; i < arracid.length; i++) {
var $el = document.querySelector('#ACID' + i));
}
Upvotes: 0
Reputation: 11112
(This is a jquery solution, since the question was initially tagged with jQuery)
You can use the selector of input elements with name starting with ^=
ACID:
$("input[name^=ACID]").each(function(){
console.log($(this).val());
});
Upvotes: 2
Reputation: 372
Since there can be more than one element with same name, so we need to get first element with that name, I have corrected your query check this, it will work.
var acidCount = document.getElementsByName('ACID'+index)[0].value
Upvotes: 1
Reputation: 25351
Since you are already assigning an ID to your inputs, it's recommended to use getElementsById
which is faster than getElementsByName
(and more accurate because the IDs are supposed to be unique, while the names don't have to be). Try this:
var acidCount = document.getElementById("ACID" + index);
If you still want to use getElementsByName
, try this:
var acidCount = document.getElementsByName("ACID" + index);
But remember that getElementsByName
returns a list of elements, but the list has only one element, because your names are unique. If you want to get that element in the list, you can use it's index like this:
var acidCount = document.getElementsByName("ACID" + index)[0];
Alternatively, if you want to get the list of all your inputs, first remove the index from the name:
cell1.innerHTML="<input type='hidden' name='ACID' class='tblrows' id='ACID"+index+"' value='"+arracid[temp]+"'>";
Then use:
var acidCount = document.getElementsByName("ACID");
Note: all the above return the DOM element(s). If you're only interested in the value, use the value
property:
var acidCount = document.getElementById("ACID" + index).value;
or
var acidCount = document.getElementsByName("ACID" + index)[0].value;
Upvotes: 2
Reputation: 41
You can try using class name.
$(document).find(".tblrows").each(function(){
console.log($(this).val());
});
Upvotes: 0
Reputation: 17910
Try using wildcard *
in the selector which will return all matched elements
document.querySelectorAll('[id*=ACID]')
Upvotes: 0
Reputation: 58573
Issue is with single quoutes and double quotes :
var acidCount = document.getElementsByName("ACID"+index)
Upvotes: 1