2

I have a button with class name test-button test-button--check. After clicking test-button--check it should do something and be replaced by class test-button--reset

For test-button--reset I want to write another function, but It doesn't work. Because, the previous function executes again.

$(".test-button--check").on("click", function() {
  alert("Check is clicked");
  $(this).removeClass("test-button--check").addClass("test-button--reset");
});

$(".test-button--reset").on("click", function() {
  alert("Reset is clicked");
  $(this).removeClass("test-button--reset").addClass("test-button--check");
});

What can I do?

Thanks

1
  • 5
    Please add some code to your question - then we can maybe tell you why it doesn't work! Commented Nov 6, 2016 at 17:14

3 Answers 3

1

You can write your code inside the document.ready in this way

$(".test-button--check .test-button--reset").on("click", function(e) {
   e.preventDefault();
   var obj=$(this);
   if($(obj).hasClass('test-button--check')){
       alert("Check is clicked");
       $(this).removeClass("test-button--check").addClass("test-button--reset");
   }
   if($(obj).hasClass('test-button--reset')){
       alert("Reset is clicked");
       $(this).removeClass("test-button--reset").addClass("test-button--check");
   }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try using .off() to remove an event handler, in this case, it is click.

Should be something like this:

$('.test-button.test-button--reset').off().click(function() {...});

Comments

1

I think this will work:

var check = function checkFunc() {
alert('Check is clicked!');
$(this).addClass('test-button--reset').removeClass('test-button--check');
$('.test-button--reset').unbind('click',check);
$('.test-button--reset').bind('click',reset);
}
var reset = function resetFunc() {
alert('Reset is clicked!');
$(this).addClass('test-button--check').removeClass('test-button--reset');
$('.test-button--check').unbind('click',reset);
$('.test-button--check').bind('click',check);
}
$('.test-button--check').bind('click',check);

Using bind and unbind

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.