Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  70] [ 1]  / answers: 1 / hits: 182094  / 15 Years ago, thu, may 14, 2009, 12:00:00

I am trying to create a checkbox dynamically using following HTML/JavaScript. Any ideas why it doesn't work?



<div id=cb></div>
<script type=text/javascript>
var cbh = document.getElementById('cb');
var val = '1';
var cap = 'Jan';

var cb = document.createElement('input');
cb.type = 'checkbox';
cbh.appendChild(cb);
cb.name = val;
cb.value = cap;
cb.appendChild(document.createTextNode(cap));
</script>

More From » html

 Answers
14

You're trying to put a text node inside an input element.



Input elements are empty and can't have children.



...
var checkbox = document.createElement('input');
checkbox.type = checkbox;
checkbox.name = name;
checkbox.value = value;
checkbox.id = id;

var label = document.createElement('label')
label.htmlFor = id;
label.appendChild(document.createTextNode('text for label after checkbox'));

container.appendChild(checkbox);
container.appendChild(label);

[#99531] Monday, May 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harrisonnelsonb

Total Points: 63
Total Questions: 112
Total Answers: 97

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;