0

How to toggle display with Javascript Vanilla when I click a checkbox.

I tried with only one element and it works but when there is more than one...I put this in my code-->

function OnlyOK(){      
var ul = document.getElementsByClassName('RQ');
        for (i = 0; i < ul.length; i++) {
              ul[i].style.display = 'none';
      }
//else ul[i].style.display = 'block';???
    }

And the case of toggle display(none,block) only one element (1 first element)...This Works!!

function OnlyOK(){  
  var ul = document.getElementsByClassName('RQ');
  ul.style.display = ul.style.display === 'none' ? '' : 'none';
}

I tried getElementsById too! but I prefer to work with class.

5
  • 1
    Are you sure you got everything ok in your HTML? It seems to work here. Commented Sep 28, 2016 at 19:18
  • yes...the first code is not working the second yes!! But I need the first Commented Sep 28, 2016 at 19:41
  • The second one cannot work ... I think you forgot the index ul[0] or similar Commented Sep 28, 2016 at 19:44
  • And the first code seems to work fine, as @Peter_Fretter already mentioned Commented Sep 28, 2016 at 19:48
  • Of course, the first example is not a toggle code because you didn't use the same code as in your second example Commented Sep 28, 2016 at 19:49

2 Answers 2

3

So basically you want to hide some elements when you click and check a checkbox and show them again after you uncheck the checkbox, right?

You can achieve such a thing like this:

function OnlyOK(flag){      
    var ul = document.getElementsByClassName('RQ');
    for (i = 0; i < ul.length; i++) {
          if (flag) {
            ul[i].style.display = 'none';
          } else {
            ul[i].style.display = 'block';
          }

     }
}

document.querySelector('#ck')
    .addEventListener('change', function(event) {
      console.log(event);
      OnlyOK(event.target.checked);
    });

With this HTML:

<ul>
   <li class="RQ">Some item to hide</li>
   <li class="RQ">Hide me</li>
   <li class="RQ">I will be gone</li>
   <li>I do not have class :(</li>
</ul>

<input type="checkbox" id="ck"/>

Here is a working example.

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

3 Comments

Cannot read property 'addEventListener' of null what can be the problem???...I put the ck id and the new javascript code..Can be because I don have jquery installed?
My example is not using jQuery so that wouldn't be the case. It seems to me from what you are telling that it doesn't find the element. Is my example from JSBin working for you?
no...I try all of ur code...the same as you but in the console it says I have an error
0
<input type="checkbox" class="eq" onchange="onlyOk(this)"></input>

function onlyOk(obj){
var rq = document.getElementsByClassName("RQ");
var i;
var display = obj.checked ? "none" : "";

        for (i = 0; i > rq.length; i++){
                rq[i].style.display = display;
        }

}

2 Comments

You can eliminate one loop using a variable: var display = obj.checked ? "none" : ""; and use it to assign to each element.
Good fix, that cut half of the function code! @Marcelo

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.