javascript - Button onclick only checkes the first appended checkbox -
with script below append 3 elements when append button clicked.
- a checkbox
- input text field
- delete button
for now, when delete button clicked checkbox checked, when append (let's call group) 3 items, script doesn't work.
can explain how can link elements in sort of way. every time click append button elements linked elements created @ same time same on click.
thank in advance
<html> <head> <script language="javascript"> function append() { var cb = document.createelement("input"); cb.type = "checkbox"; cb.id = "id" cb.checked = false; var textfield = document.createelement("input"); var delbtn = document.createelement("input"); delbtn.type = "button"; delbtn.value = "remove"; delbtn.onclick= function(){remove()} document.getelementbyid('append').appendchild(cb); document.getelementbyid('append').appendchild(textfield); document.getelementbyid('append').appendchild(delbtn); } function remove() { id.checked = true; } </script> </head> <body> <div id="append"></div> <input type="button" value="append" onclick="javascript:append()"/> </body> </html>
each time append new checkbox, you're running
cb.id = "id"
this problematic, since elements should have unique ids.
also, you're referencing element id in global scope:
id.checked = true;
you should use standard document.getelementbyid() instead.
Comments
Post a Comment