admin管理员组文章数量:1387332
When I implement this code - the name of the checkbox wont show up in the browser alongside the checkbox - just the checkbox itself. whats the reason for this? Am I using the setattribute-function incorrectly?
<script type="text/javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
document.body.appendChild(x);
</script>
When I implement this code - the name of the checkbox wont show up in the browser alongside the checkbox - just the checkbox itself. whats the reason for this? Am I using the setattribute-function incorrectly?
<script type="text/javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
document.body.appendChild(x);
</script>
Share
Improve this question
asked May 15, 2016 at 20:09
javajava
1,1652 gold badges29 silver badges52 bronze badges
2
-
1
The code works fine: jsfiddle/ag663kw8
name
will not add the textvehicle
next to your<input />
; that's what<label>
is for. – BenM Commented May 15, 2016 at 20:13 - Do you try to add a label? – Ygalbel Commented May 15, 2016 at 20:15
2 Answers
Reset to default 5You need to add a label
element:
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
document.body.appendChild(x);
var label = document.createElement("label");
label.textContent = "vehicle";
document.body.appendChild(label);
You should create a label for the check box as you created checkbox dynamically and append it to body
//create checkbox
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
//create label
var y = document.createElement("label");
y.innerHTML = "Here goes the text";
//Append Them to body
document.body.appendChild(x);
document.body.appendChild(y);
本文标签: domJavascript setattributename and value not workStack Overflow
版权声明:本文标题:dom - Javascript setattribute - name and value not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744497181a2609099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论