Rick Thompson
Rick Thompson

Reputation: 47

how to make following jquery function repeat itself

This function repeats 2 times i.e when I click on .search-toggle 2 times, z-index of .nav-secondary toggles between 0 and 2. But I want it to toggle in loop without breaking anything. Any help is appreciated.

enter code here

jQuery(document).ready(function($){

$(".search-toggle").click(function() {

$(".nav-secondary").css("z-index",0); 

$(".search-toggle").click (function() {

 $(".nav-secondary").css("z-index",2);

 $(".search-toggle").click(function() {

    $(".nav-secondary").css("z-index",0); 

    $(".search-toggle").click (function() {

      $(".nav-secondary").css("z-index",2);

    });
   });      
  });
 });
});

Upvotes: 0

Views: 48

Answers (1)

xxMrPHDxx
xxMrPHDxx

Reputation: 667

It seems like you want to toggle the value between 0 and -2. You can just store the z-index value and invert it when click event fired. Something like

jQuery(document).ready(function($){ 
    let a; (a = $(".nav-secondary")).css("z-index",2);
    $(".search-toggle").click(function() {
        a.css("z-index", 0-parseInt(a.css("z-index"))); 
    })
})

Keep in mind that the toggle value changed after 1 click and switches between 2 and -2

Upvotes: 1

Related Questions