javascript - How do I remove toggleClass on click and when any other element and white space is clicked? - Stack Overflow

I am using JQuery toggleClass to change the color of a button when it's clicked.When it's c

I am using JQuery toggleClass to change the color of a button when it's clicked. When it's clicked again it should revert to the original color AND when the mouse is clicked anywhere else including white space, it should revert to the original color.

I couldn't figure out how to do it but below are two snippets that I managed to find and plug stuff in.

The following snippet will change the color when button is clicked but when clicked again it doesn't revert to original color but when any other element or a white space is clicked, the button color reverts to the original color:

  (function () {
$('.button_is').click(function(evt) {
    evt.stopPropagation(); //stops the document click action
    $(this).siblings().removeClass('button_addition');
    $(this).toggleClass('button_addition');
});


$(document).click(function() {
    $('.button_is').removeClass('button_addition'); //make all inactive
});
});

The following snippet will toggle class when the element is clicked and when it's clicked again it reverts to it's original state but wont' revert to original color if clicked elsewhere:

 $(document).ready(function () {

            $(".button_is").click(function () {
                $(this).toggleClass("button_addition");
            });
        });

Which of these two snippets can I utilize to make sure that when the button is clicked the color changes and when it's clicked again it reverts to the original color AND when other elements or white space is clicked it reverts to the original color?

I am using JQuery toggleClass to change the color of a button when it's clicked. When it's clicked again it should revert to the original color AND when the mouse is clicked anywhere else including white space, it should revert to the original color.

I couldn't figure out how to do it but below are two snippets that I managed to find and plug stuff in.

The following snippet will change the color when button is clicked but when clicked again it doesn't revert to original color but when any other element or a white space is clicked, the button color reverts to the original color:

  (function () {
$('.button_is').click(function(evt) {
    evt.stopPropagation(); //stops the document click action
    $(this).siblings().removeClass('button_addition');
    $(this).toggleClass('button_addition');
});


$(document).click(function() {
    $('.button_is').removeClass('button_addition'); //make all inactive
});
});

The following snippet will toggle class when the element is clicked and when it's clicked again it reverts to it's original state but wont' revert to original color if clicked elsewhere:

 $(document).ready(function () {

            $(".button_is").click(function () {
                $(this).toggleClass("button_addition");
            });
        });

Which of these two snippets can I utilize to make sure that when the button is clicked the color changes and when it's clicked again it reverts to the original color AND when other elements or white space is clicked it reverts to the original color?

Share Improve this question asked Apr 16, 2013 at 18:49 AAAAAA 3,16811 gold badges55 silver badges73 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

You may try this

$(document).ready(function () {

    $(".button_is").click(function (e) {
        e.stopPropagation();
        $(this).toggleClass("button_addition");
    });

    $(document).click(function() {
        $(".button_is.button_addition").trigger("click");
    });    

});

DEMO.

Try this

 $(function() {
        $('.button_is').on('click',
        function(event) {
            event.stopPropagation();
            $('.button_is').toggleClass('button_addition');
        });
        $('html').click(function() {
            $('.button_is').removeClass('button_addition');
        });
    });

Fiddle: http://jsfiddle/9ZKfq/

$(document).ready(function() {

    $(document).click(function(jEvent) {    

        if (!$(jEvent.target).hasClass('button_is')) 
        {
           $('.button_is').removeClass('button_addition');
        }
        else
        {
            $(jEvent.target).toggleClass('button_addition');
        }

    });

});

Try this -

    $(document).ready(function () {
        $(".button_is").click(function () {
            $(this).toggleClass("button_addition");
        });

        $(document).click(function(event) {
          if(!$(event.target).is('.button_is')){
            $('.button_is').removeClass('button_addition'); //make all inactive
          }
        });
  });

Your first snip works for me.

Maybe your HTML is the problem?

It's worth noting that there is a typo in your code (you're missing the $ on the ready handler).

Look at this: http://jsfiddle/URrVf/ It works on Chrome, Firefox and IE9.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744814921a4595265.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信