1

I have a number of buttons that I'm adding/removing a class on click. My script turns all the buttons off before turning the clicked button on. The problem with this is one button is always set to on when I would like the ability to turn it off when it's clicked a second time. I've tried using if statements and is(), but nothing's worked yet. Can someone lend me a hand?

$('.silver_button').live('click', function () {
  $('.checked').text('Select').removeClass('checked');
  $(this).text("").addClass('checked');

  if ($('.challenge_card .silver_button').is('.checked')) {
    $('.silver_button').text("Select").removeClass("checked");
  }

});
2
  • Is .challenge_card the identifier for your special case button? Does it also inherit the .silver_button class? Do you want your special case button to stay enabled unless it's clicked specifically? Commented Oct 10, 2012 at 22:59
  • Its quite hard to understand what you want to do. Perhaps because the meaning in the classes means nothing without the context. You click a button and all the buttons clear their text value except the clicked button? However what happens when you click your challenge_card? The same as the others, or nothing, or only after 2 clicks? Commented Oct 10, 2012 at 23:01

1 Answer 1

1

Try this:

$('.silver_button').live('click', function () {

    if ($(this).hasClass('checked')) {

        $(this).text("Select").removeClass("checked");

    } else {

        $('.checked').text('Select').removeClass('checked');
        $(this).text("").addClass('checked');

    }

});
Sign up to request clarification or add additional context in comments.

Comments

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.