Short question to keydown/.hasClass/.setClass

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.

  1. $(document).ready(function()
  2. { $("*").on("keydown", function(e){
  3. var test = $("#id1").clone();
  4. if(e.which == 83){
  5. if($("#id1").hasClass("invisible")){
  6. $("#id1").removeClass("invisible").addClass("visible");
  7. console.log("Removing invisible-setting visible");
  8. } else if(test.hasClass("visible")) {
  9. $("#id1").removeClass("visible").addClass("invisible");
  10. console.log("Removing visible-setting invisible");
  11. } else {
  12. console.log(e.which);
  13. }
  14. });
  15. });

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