GetElementsByName with array like name

流过昼夜 提交于 2019-12-04 09:03:22
<input name="color[3]" id="color_3" type="text" />

var element = document.getElementsByName("color[3]");

alert(element[0].id);

It works fine .. The thing you should have in your mind is Return type is an array of elements not a single element

If you want all of the color inputs, you can use querySelectorAll instead to query for the name attribute:

document.querySelectorAll("input[name^='color[']")

This looks through the document for all input tags whose name attribute starts with color[. Here is a fiddle for this.

If you only want color[3], you can use:

var color3 = document.getElementsByName("color[3]");
console.log(color3[0]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!