Jquery toggle id instead of classes?

Is there a way to make a switch function that primarily includes only one CSS style element, such as a background color or something like that. and which selects id instead of class because I know toggleClass, but they just wonder if this is possible with identifiers?

$("#gallery").load('http://localhost/index.php/site/gallerys_avalible/ #gallerys_avalible'); $('li').live('click', function(e) { e.preventDefault(); if(!clickCount >=1) { $(this).css("background-color","#CC0000"); clickCount++; } console.log("I have been clicked!"); return false; }); 
+6
source share
2 answers

EDIT:

After reading the comment on the OP - I believe that this is what he is looking for a way to highlight the โ€œactiveโ€ link when clicked. And yes, teresko is definitely right that you should switch classes, not identifiers.

This is the essence of the jQuery snippet you might be looking for:

 $("li").bind('click', function(){ // remove the active class if it there if($("li.active").length) $("li.active").removeClass('active'); // add teh active class to the clicked element $(this).addClass('active'); }); 

Demo


Check out jQuery toggle api.

This is a bit confusing because a simple google search in jQuery toggle brings you to the show / hide toggle documentation. But .toggle() can be used to alternate functions - you can even add more than two.

like so...

 $("el").toggle( function(){ $(this).css('background-color', 'red'); }, function(){ $(this).css('background-color, ''); // sets the bg-color to nothing }); 
+2
source

You really should use classes for this. The identifiers are unique on the page and should be used as points where you capture events (via $.live() or some other method that uses event delegation). Also, if you are thinking about using identifiers because they have higher specificity in CSS rules, then you are going wrong.

In short: a bad idea, stick with switch classes.

+3
source

Source: https://habr.com/ru/post/897187/


All Articles