Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle CSS?

Tags:

jquery

css

toggle

I want to toggle between CSS so when a user clicks the button (#user_button) it shows the menu (#user_options) and changes the CSS, and when the user clicks it again it goes back to normal. So far this is all I have:

$('#user_button').click( function() {     $('#user_options').toggle();     $("#user_button").css({             borderBottomLeftRadius: '0px',         borderBottomRightRadius: '0px'     });      return false; }); 

Can anybody help?

like image 872
Edd Turtle Avatar asked Jul 26 '10 18:07

Edd Turtle


People also ask

Can you toggle CSS in jQuery?

jQuery toggle() MethodThe toggle() method was deprecated in jQuery version 1.8, and removed in version 1.9.

What is toggle () in jQuery?

jQuery toggle() Method The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.

How do I toggle icons in jQuery?

click(function(){ $('#display_advance'). toggle('1000'); $(this).


1 Answers

For jQuery versions lower than 1.9 (see https://api.jquery.com/toggle-event):

$('#user_button').toggle(function () {     $("#user_button").css({borderBottomLeftRadius: "0px"}); }, function () {     $("#user_button").css({borderBottomLeftRadius: "5px"}); }); 

Using classes in this case would be better than setting the css directly though, look at the addClass and removeClass methods alecwh mentioned.

$('#user_button').toggle(function () {     $("#user_button").addClass("active"); }, function () {     $("#user_button").removeClass("active"); }); 
like image 73
Ian Wetherbee Avatar answered Sep 19 '22 16:09

Ian Wetherbee