dimanche 1 janvier 2017

When checkbox is checked, add new class to divs that don't have a the checked class

I posted a little while ago about adding display: none to divs that aren't selected by the checkbox and was advised to think about re-writing the code.

So when:

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

is checked. I want jQuery to check all div elements with the class .guide-block. The divs with .guide-block but without the class .install I want hidden. To do so I tried adding display: none to them.

In short, it doesn't work. I'm new to jQuery and java so I'm learning as I go. At this point if you tell me to stop I will. I think I'm heading down a rabbit hole with no end because I don't have a map. Thanks in advance!

$(document).ready(function(){
    $('#install').change(function () {
        if (this.checked) {
            if (!$('div.guide-block').hasClass('install')) {
                $('div.guide-block').css("display", "none");
            };
        };
    });
});

This is how I read this code in my head.

Pseudo code

Adding a function to the id tag #install
   if the checkbox is checked {
     check all divs with the class .guide-block. If the divs with .guide-block don't have the class .install {
        add the class display: none;
     }
   }
}




Aucun commentaire:

Enregistrer un commentaire