81

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?

1
  • 2
    Can you post your HTML/CSS snippets? Commented Jul 26, 2010 at 18:11

9 Answers 9

128

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");
});
Sign up to request clarification or add additional context in comments.

3 Comments

This no longer works as this method was deprecated in jQuery 1.8 and removed in jQuery 1.9. See api.jquery.com/toggle-event
But what if we have transition effect? in this case transition effect won't be applied, right?
This way it just adds the d-none (the boostrap class) to my div element, so I use $('#user_button').toggleClass('active'); whenever button is clicked.
75

I would use the toggleClass function in jQuery and define the CSS to the class e.g.

/* start of css */
#user_button.active {
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
    -webkit-border-bottom-right-radius: 5px; /* user-agent specific */
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-bottomright: 5px;
    -moz-border-radius-bottomleft: 5px; /* etc... */
}
/* start of js */
$('#user_button').click(function() {
    $('#user_options').toggle();
    $(this).toggleClass('active');
    return false;
})

5 Comments

This is what I'd do too. Less styling in your JS then
Why is this not the answer?
@Ties - It definitely pays to be the first to post. It's a shame because it forcibly has a negative impact on the quality of answers. Better a quick response than a thorough one.
Both are right answers, just depends on the method you want to use. I like this one though, thanks
Anyone reading through this. Please consider using this answer before using the answer marked for the solution.
4

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.

1 Comment

toggleClass is equivalent to these unless you want to remove a different class than you are adding for some reason
2

The initiale code must have borderBottomLeftRadius: 0px

$('#user_button').toggle().css('borderBottomLeftRadius','+5px');

Comments

1

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"); 

Comments

0

You can do this by maintaining the state as below:

$('#user_button').on('click',function(){
    if($(this).attr('data-click-state') == 1) {
        $(this).attr('data-click-state', 0);
        $(this).css('background-color', 'red')
      }
    else {
      $(this).attr('data-click-state', 1);
      $(this).css('background-color', 'orange')
    }
  });

Take a reference from the codepen example here

Comments

-1
$('#user_button').on('click', function() {
  $("#user_button").toggleClass('active');
});
#user_option.active {
  border-bottom-right-radius: 5px;
  border-bottom-left-radius: 5px;
}

1 Comment

This just repeats existing answers without any context or explanation.
-1
$(document).ready(function(){

    $('#toggle').active('click', function(){
        $(".navbar-collapse").addClass("show");
    });

    $('#toggle .active').on('click', function(){
        $(".navbar-collapse").removeClass("hide");
    });

});

The Toggle menu has been closed and opened but the Navigation item is not closed.

1 Comment

This answer seems very similar to the already accepted answer.
-2
$('#user_button').click(function () {  
  $("#user_button").toggleClass("active");
});

1 Comment

Please provide an explanation of how this code solves the question. This will help the OP and future searchers learn from your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.