Short question to keydown/.hasClass/.setClass
Hello,
I'm new to jQuery and want to learn a little bit more :-).
In my example I have a <div> with the id "box1" containing three <p>s.
Now I want to get the div visible/invisible by pressing the key "s".
First of all I created two css-classes:
.invisible{ visibility: hidden; }
.visible{ visibility: visible; }
and added invisible to my div with the id box1.
- $(document).ready(function()
- { $("*").on("keydown", function(e){
- var test = $("#id1").clone();
- if(e.which == 83){
- if($("#id1").hasClass("invisible")){
- $("#id1").removeClass("invisible").addClass("visible");
- console.log("Removing invisible-setting visible");
- } else if(test.hasClass("visible")) {
- $("#id1").removeClass("visible").addClass("invisible");
- console.log("Removing visible-setting invisible");
- } else {
- console.log(e.which);
- }
- });
- });
Now when I press in my browser the key "s" my div is not getting visible and the console says
"Removing invisible-setting visible
Removing visible-setting invisible"
Where is the problem? Can you please help me?
Thank you!
Timon