admin管理员组文章数量:1326447
A page has elements without id - only with names. How to access them by javascript like this?
document.getElementById('id').value := "...";
UPDATED:
Thanks to all! Is it possible to access then to element with specified value (radiobutton)?
<nobr><label><input style="vertical-align:middle" type="radio" name="name" value="value1" checked="checked"/><span style="vertical-align:middle" class="label">value1</span></label></nobr><br/>
<nobr><label><input style="vertical-align:middle" type="radio" name="name" value="value2"/><span style="vertical-align:middle" class="label">value2</span></label></nobr><br/>
A page has elements without id - only with names. How to access them by javascript like this?
document.getElementById('id').value := "...";
UPDATED:
Thanks to all! Is it possible to access then to element with specified value (radiobutton)?
<nobr><label><input style="vertical-align:middle" type="radio" name="name" value="value1" checked="checked"/><span style="vertical-align:middle" class="label">value1</span></label></nobr><br/>
<nobr><label><input style="vertical-align:middle" type="radio" name="name" value="value2"/><span style="vertical-align:middle" class="label">value2</span></label></nobr><br/>
Share
Improve this question
edited Dec 9, 2011 at 5:35
Dmitry
asked Dec 9, 2011 at 5:06
DmitryDmitry
14.6k24 gold badges112 silver badges194 bronze badges
0
4 Answers
Reset to default 6document.getElementsByName('name')
This returns a nodeList, which you can access like an array.
There is the DOM HTML getElementsByName method that returns a collection of nodes with that name. Note that the name property is not required to be unique, so you may get more than one and that the returned collection is live, so adding or removing elements with the subject name will modify the collection.
use document.getElementsByName('elementName')
. this will give you an array-like collection, get the element by index
The other answers cover your original question. Regarding your update about accessing by value, there is no getElementsByValue()
function, but if it is your intention to select the particular radio button that has both a specified name and value you can do something like this:
function getElementByNameAndValue(n, v) {
var els = document.getElementsByName(n),
i;
for (i=0; i < els.length; i++)
if (els[i].value == v)
return els[i];
return null;
}
var r = getElementByNameAndValue("name", "value1");
Above function will return a reference to the first matching element or null if nothing matched. If you want to allow for more than one element with the same name and value you could modify this to return an array (though I don't know why you'd have more than one element with the same name and value, at least not with radio buttons).
Note that also this can easily be modified to return a reference to the currently checked element, just change the if in the loop to be if (els[i].checked)
.
You may be able to do some or all of this using document.querySelector()
, but I don't use it myself because I need to support older IE. You can definitely replace all of the above with one line of jQuery.
本文标签: How do access to element by name in javascriptStack Overflow
版权声明:本文标题:How do access to element by name in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213552a2434154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论