Getting the "id" value of a control in javascript
I have the following code below, that is working开发者_如何学JAVA for the most part. It returns the value in the textbox control. However, I also need the control's "id" value (the textbox controls name). How can I obtain that?
When I do an "inputs[i]." I only get item and value options.
var brSettings = document.getElementById("divSettings");
var inputs = brSettings.getElementsByTagName("input");
var sum = 0
for (var i = 0; i < inputs.length; i++) {
sum += inputs[i].value;
}
You just need to access the id like so:
inputs[i].id
Simples!
inputs[i].id
And some extra stuff here to satisfy post length. sigh
inputs[i].getAttribute("id")
should do the trick
http://www.javascriptkit.com/dhtmltutors/domattribute.shtml
Use the getAttribute()
method:
inputs[i].getAttribute("id");
You could use inputs[i].id
to get the id - http://developer.mozilla.org/en/DOM/element.id
and
inputs[i].name
to get the name - http://developer.mozilla.org/en/DOM/element.name
If you were using ASP.Net 4.0 you could set the ClientID=Static and then the name becomes strongly typed. However, if you are using an older version of the Framework then you can use wildcards. *"inputs" which would give you all the elements that contain "inputs" in the id name.
精彩评论