Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  3] [ 3]  / answers: 1 / hits: 40573  / 13 Years ago, mon, september 12, 2011, 12:00:00

is there a way to make a toggle function that first of all toggles only one css style element, such as background-color or something like that. and that selects an id instead of a class, for i know of the toggleClass, but im just wondering if it's possible with ids instead?



$(#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;
});

More From » jquery

 Answers
41

EDIT:



After reading OP's comment - I believe this is what he is looking for a way to highlight an active link on click. And Yes, teresko is definitely right that you should be toggling the classes, not the ID's.



This is the essence of a jQuery snippet that you may be looking for:



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


Demo






Check out the jQuery toggle api.



It's a little confusing because a simple google search on 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
});

[#90136] Saturday, September 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorafrancisl

Total Points: 80
Total Questions: 96
Total Answers: 102

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
;