Reputation: 919
Having a confusing issue with jQuery, basicly this code is added to 2 elements, and works fine, but I wan it to NOT work if you click a seperate element, I thought removing the class from these elements would stop them from workign, but even with no class they are still working... here some code.
$(".q1a1, .q1a3").click(function (e) {
e.preventDefault();
$(this).animate({ backgroundColor: "#db384b" }, 1000);
$(this).animate({ backgroundColor: "#0F437D" }, 2000);
$(this).children('.wrong').fadeIn(1000).fadeOut(2000);
});
^ I want this code to not work after the code below is clicked (if that makes sense)
$(".q1a2").click(function (e) {
e.preventDefault();
$(this).animate({ backgroundColor: "#00a6eb" }, 800);
$('.q1a2 .correct').show();
$('.q1a1, .q1a3 ').removeAttr("class");
});
Any ideas?
Upvotes: 0
Views: 155
Reputation: 4092
Removing the class will not help, as you have already attached the click event to the element.
You should use unbind to remove
$('.q1a1, .q1a3 ').unbind("click");
Upvotes: 0
Reputation: 46
Try doing a .unbind(). Looks something like this.
$('.q1a1, .q1a3 ').unbind('click');
You can read more about .unbind() http://api.jquery.com/unbind/
Upvotes: 1
Reputation: 9092
You can declare your handler in a separate function, something similar to this:
var SomeFunction = function(e) {
e.preventDefault();
// your function code goes here
$(this).unbind(SomeFunction); // and this removes the handler from the element on the first click
};
$(".q1a1, .q1a3").click(SomeFunction);
Upvotes: 0