tags:

views:

78

answers:

5
+2  Q: 

jQuery toggle CSS?

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?

+3  A: 

You might want to use jQuery's .addClass and .removeClass commands, and create two different classes for the states. This, to me, would be the best practice way of doing it.

alecwh
+8  A: 
$('#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");
});
Ian Wetherbee
+1  A: 

The best option would be to set a class style in CSS like .showMenu and .hideMenu with the various styles inside. Then you can do something like

$("#user_button").addClass("showMenu"); 
Munzilla
+1  A: 

i would use the toggleClass function in jquery and define the css to the class e.g.

/* start of css */
#user_button.active {
    -webkit-border-bottom-right-radius: 5px;
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-bottomright: 5px;
    -moz-border-radius-bottomleft: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}
/* start of js */
$('#user_button').click(function() {
    $('#user_options').toggle();
    $(this).toggleClass('active');
    return false;
}
This is what I'd do too. Less styling in your JS then
DavidYell
A: 

Thanks Everyone - That's exactly what I needed, I used the toggleClass function :)

Edd Turtle