html - How can I get element state from appendchild checkbox in Javascript? -
html - How can I get element state from appendchild checkbox in Javascript? -
i creating checkboxes through appendchild. want able check state. know format supposed document.form.checkboxname.value no matter combination of form name or checkbox name utilize doesn't give me response.
var myparagraph=document.getelementbyid("myline"); myform = document.createelement("form"); myform.setattribute("name", "myform2"); mytable = document.createelement("table"); var cb_delete = document.createelement('input'); cb_delete.type = "checkbox"; cb_delete.name = "cb_test"; cb_delete.setattribute("name", "cbtest2"); cb_delete.value = 2; cb_delete.onclick = answer; thecell.appendchild(cb_delete);
to recap want reply function give me alert if checkbox checked or not. give thanks much! clarify replace form , checkbox in alert(document.form.checkbox.value)?
cb_delete.checked
should work. see htmlinputelement on mdn.
console.log(cb_delete.checked);
jsfiddle demo
to create checkbox checked or unchecked, cb_delete.checked = true
or cb_delete.checked = false
respectively. checked
object property expected have boolean values. cb_delete.checked
queries current value.
in event handler function, can reference element handles event this
. illustration in previous jsfiddle:
cb_delete.onchange = function () { console.log(this.checked); /* refers checkbox here */ };
you utilize this
in answer
function well.
you can read more handling events on quirksmode.
note: using cb_delete.onchange
quite old , not recommended way attach event handlers of time. utilize of cb_delete.addeventlistener('change', answer, false)
right way this. older ies have problem it, have attachevent()
instead. can read this technique , why utilize on quirksmode (the article 3 years old, take business relationship when reading things "unfortunately few browsers back upwards @ moment."
). hide browser differences, can utilize library jquery.
javascript html forms
Comments
Post a Comment