Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add/remove a class or style with jquery

Tags:

jquery

css

I have the following code which shows nested ul and hides the other open ones on click. my question is how can i add a background image to the parent li a that opens the nested ul and removes the background image from the parent li a of the ones it closes?

here is my jquery:

$(document).ready(function() {
    $('ul ul').hide();

    $('ul li > a').click(function(event) {
        $('ul ul').hide('slow');
        $(this).parent().find('ul').show('slow');
    });

});;
like image 683
user875293 Avatar asked Dec 09 '22 03:12

user875293


1 Answers

// will add the class
$('#item').addClass('myClass');

// will remove the class
$('#item').removeClass('myClass'); 

// will toggle the class (add it if doesn't have it or remove it if it does)
$('#item').toggleClass('myClass'); 

and inline styles;

// will override those properties
$('#item').css({'color':'red','background':'blue'}); 
like image 176
Toni Michel Caubet Avatar answered Dec 15 '22 01:12

Toni Michel Caubet