2

I have several checkboxes and divs. I would like to toggle the divs based on the checkboxes. I tried toggling the divs using ternary but it isn't working as I expected. Below is the js. I do not want to use jQuery.

function toggle(flavor){
  var checkBox = document.getElementById(flavor)
  checkBox === true ? document.getElementById(flavor+"Div").style.display = "block" : document.getElementById(flavor+"Div").style.display = "none";
}

Here's the error I get: pen.js:4 Uncaught TypeError: Cannot read property 'style' of null

Here's the pen

Thanks for any help you can provide.

1
  • It's a case issue your id's on your checkboxes are capital first letter and the divs are lower case first letter. Commented Nov 19, 2020 at 0:45

3 Answers 3

2

Like was mentioned in a comment, you have a case issue that needs fixing 'Vanilla' -> 'vanilla' etc.

Also, maybe this will help on the ternary operator (it took me a while to get my head around it): typically, you want to use the ternary to return something, not just to toggle between executing two different conditions.

ie. let newVar = checkSomething === checkAgainst ? this : that

And since you're using a .hideDiv class already for setting display style, you can use the element.classList.toggle(className) method to switch between the two states and let vanilla js handle the boolean in the background.

I left the console.log(thing) in the snippets just because it helped me follow what was what at each point.

function toggle(flavor){
  var checkBox = document.getElementById(flavor)
  console.log(checkBox)
  
  let divElement = document.getElementById(flavor+"Div")
  console.log(divElement)
  
  divElement.classList.toggle('hideDiv')
  }
.hideDiv{
  display:none
}
<input type="checkbox" id="vanilla" onClick="toggle('vanilla')"/> Vanilla
<input type="checkbox" id="cherry"  onClick="toggle('cherry')"/> Cherry
<input type="checkbox" id="apple" onClick="toggle('apple')" /> Apple

<div id="vanillaDiv" class="hideDiv">
<textarea  id="vanComments"></textarea>
</div>

<div id="cherryDiv" class="hideDiv">
<textarea id="cherComments"></textarea>
</div>

<div id="appleDiv" class="hideDiv">
<textarea id="appComments"></textarea>
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

You have some typos on your pen. JavaScript is case sensitive, so vanillaDiv and VanillaDiv are not the same thing. All your DIVs were starting with lowercase while your checkboxes were in uppercase, therefore you would never be able to correctly match the element.

EDIT: (just for the record) To correctly verify if a checkbox is checked or not, you need to use the .checked property. (forgot to write this part of text, although the code I posted had this correction since the beginning).

So change :

<div id="vanillaDiv" class="hideDiv">
<textarea  id="vanComments"></textarea>
</div>

to :

<div id="VanillaDiv" class="hideDiv">
<textarea  id="vanComments"></textarea>
</div>

Also, the use of the .checked property for the checkbox and a simpler way for the ternary would be :

function toggle(flavor){
  var checkBox = document.getElementById(flavor).checked;
  document.getElementById(flavor+"Div").style.display = checkBox === true ? "block" : "none";
}

Comments

1

There are two issues:

  1. The html event should have the same case level of target, you're current trying to get a id like 'VanillaDiv' instead of 'vanillaDiv' present in DOM.

  2. The javascript code need to check the element.checked property instead of asking for the element === true

function toggle(flavor) {
  const checkbox = document.getElementById(flavor);
  const textarea = document.getElementById(flavor+"Div");
  // here you need to use .checked instead of element
  textarea.style.display = checkbox.checked ? "block" : "none";
}
.hideDiv {
    display: none
}
<input type="checkbox" id="vanilla" onClick="toggle('vanilla')"/> Vanilla
<input type="checkbox" id="cherry" onClick="toggle('cherry')"/> Cherry
<input type="checkbox" id="apple" onClick="toggle('apple')" /> Apple

<div id="vanillaDiv" class="hideDiv">
<textarea id="vanComments">vanilla</textarea>
</div>

<div id="cherryDiv" class="hideDiv">
<textarea id="cherComments">cherry</textarea>
</div>

<div id="appleDiv" class="hideDiv">
<textarea id="appComments">apple</textarea>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.