Bhagirath
Bhagirath

Reputation: 161

Checkbox not checked onclick JavaScript

I'm trying to add an 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 JavaScript.

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>

I also tried this in JsFiddle which gives the same error and am not sure what I'm doing wrong.

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

Upvotes: 3

Views: 4712

Answers (6)

Sieg
Sieg

Reputation: 564

When use innerHTML all events of the element is canceled.

You need to use DOM functions.

<html>
<div id="test_div">
    <input type="checkbox" name="test_checkbox" id="test_checkbox" onchange="check_test()" />
</div>
<script>
    function check_test() {
        var testdiv = document.getElementById("test_div");
        if (!document.contains(document.getElementById("test_number"))) {

            var newInput = document.createElement('input');
            newInput.id = 'test_number';
            testdiv.appendChild(newInput);
        }else{
            document.getElementById("test_number").remove();
        }
    }

</script>

</html>

related: https://stackoverflow.com/a/595825/5667488

Upvotes: 0

Scrimothy
Scrimothy

Reputation: 2536

You're conditionally removing the #test_input if it exists in the DOM, but then you're not using an else when adding it. So no matter which state you're in, you'll always end the function with having added the input to the DOM.

As others have mentioned, when you += on the innerHTML, then you're actually creating a whole new string, thereby reinitializing your original checkbox to unchecked.

You may want to just append a new child to the wrapper. I've also used the onchange event instead so that it will do what you want no matter if the box is checked by a click or programmatically.

function check_test(checkbox) {
  const checked = checkbox.checked; // is it checked?
  const testInput = document.getElementById("test_input"); // test_input element
  
  // if it's checked and doesn't have an input, add it
  if (checked && !testInput) {
    const newInput = document.createElement('input');
    newInput.type = 'text';
    newInput.name = 'test_input';
    newInput.id = 'test_input';
    checkbox.parentNode.appendChild(newInput);
  }
  // otherwise, if it's not checked and there is an input in the DOM, remove it
  else if (!checked && testInput) {
    document.getElementById("test_input").remove();
  }
}
<div id="test_div">
  <input type="checkbox" name="test_checkbox" id="test_checkbox" onchange="check_test(event.target)" />
</div>

Upvotes: 2

HARDCODE Studios
HARDCODE Studios

Reputation: 1

Try adding an event in the function declaration:

function check_test(e)

Then calling e.checked; at the top or bottom of the function.

Let me know if that works. Answering from my phone so I can't test myself.

Upvotes: 0

sasha
sasha

Reputation: 21

By doing += you're overriding previous checkbox.

Upvotes: 2

Ahm23
Ahm23

Reputation: 366

You could use:

document.getElementById("test_div").insertAdjacentHTML("afterend", "<input type='text' name='test_input' id='test_input'/>");

Instead of:

document.getElementById("test_div").innerHTML += "<input type='text' name='test_input' id='test_input'/>";

Not the greatest solution; however, it works and it's extremely simple. Then you just fix up the rest of the page with CSS styling.

Upvotes: 0

Nikki9696
Nikki9696

Reputation: 6348

You're overwriting the content of the same div that the checkbox lives in, using innerHTML like that. Use a second div, or use create element and append child instead of replacing the entire contents.

This works.

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

Upvotes: 3

Related Questions