admin管理员组文章数量:1323683
im trying to get an element by name in my form, but for some reason im getting this error:
Uncaught TypeError: frm.getElementsByName is not a function
Here is my code:
function doc(id) { return document.getElementById(id); }
function switchFields() {
var e = doc("slcSubmit");
var sel = e.options[e.selectedIndex].value;
var frm = doc("frmSendmessage");
var messageFields = [frm.getElementsByName("name"),frm.getElementsByName("email")]; //List of objects
//Give each object a new class
for (var i=0;i<messageFields.length;i++) {
messageFields[i].class = "test";
}
}
im trying to get an element by name in my form, but for some reason im getting this error:
Uncaught TypeError: frm.getElementsByName is not a function
Here is my code:
function doc(id) { return document.getElementById(id); }
function switchFields() {
var e = doc("slcSubmit");
var sel = e.options[e.selectedIndex].value;
var frm = doc("frmSendmessage");
var messageFields = [frm.getElementsByName("name"),frm.getElementsByName("email")]; //List of objects
//Give each object a new class
for (var i=0;i<messageFields.length;i++) {
messageFields[i].class = "test";
}
}
Share
asked Aug 25, 2015 at 20:56
Martyn BallMartyn Ball
4,8959 gold badges63 silver badges145 bronze badges
2 Answers
Reset to default 9getElementsByName
is not a function that can be applied to any node (only to document
).
I think what you are looking for is frm.querySelector('[name="TheName"]')
If you need a substitute for getElementsByName that does not require Jquery, here is a function that you can use.
//g is the dom element, cl is the value for the name attribute
function getElementsByName(g, cl) {
var e = [], b = g.childNodes, a, b, f, k;
for (a = 0; a <= b.length - 1; a += 1) {
if (b[a].getAttribute) {
if (cl == b[a].getAttribute("name")) {
e.push(b[a])
}
}
f = getElementsByName(b[a], cl);
for (k = 0; k <= f - 1; k++) {
e.push(f[k])
}
}
return e
}
本文标签: javascriptJS GetElementsByName from within formStack Overflow
版权声明:本文标题:javascript - JS: GetElementsByName from within form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140664a2422566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论