vendredi 3 janvier 2020

Checkbox not checked onclick Javascript

I'm trying to add a input field on click of checkbox and I want the checkbox to be checked (which is its default behaviour!) but the checkbox is not getting checked even after the input field appears. The following is my HTML code with JS.

function check_test() {
  if (document.contains(document.getElementById("test_input"))) {
    document.getElementById("test_input").remove();
  }
  document.getElementById("test_div").innerHTML += "<input type='text' name='test_input' id='test_input'/>";
}
<div id="test_div">
  <input type="checkbox" name="test_checkbox" id="test_checkbox" onclick="check_test()" />
</div>

Also tried this in JsFiddle which gives the same error. Not sure what I'm doing wrong.

https://jsfiddle.net/1yLb70og/1/




Aucun commentaire:

Enregistrer un commentaire