I'm trying to do something very simple. Basically I have a clickable div 'hot spot', when you click that it fills the screen and displays some content. I achieved this by simply changing the class of div, removing 'spot' and adding 'grown' and there's a little CSS animation to make it grow. This works fine.
The problem is, within this div there is a close_button, which at the moment is just text. I want this to switch the classes back - i.e. remove grown and readd spot. It doesn't do this when clicked. I believe it's to do with the element not having those classes when the DOM loads, but I'm new to jQuery and don't know how to work around this.
I think there's probably a much more sensible way of doing it, could someone point me in the right direction? I'd be very grateful. I've tried using toggleClass instead to no avail.
$( document ).ready(function() {
$(".clickable").click(function() {
$(this).addClass("grown");
$(this).removeClass("spot");
});
$(".close_button").click(function() {
alert (this);
$("#spot1").removeClass("grown");
$("#spot1").addClass("spot");
});
});
UPDATE:
I am using this code now,
$( document ).ready(function() {
$(document).on("click", ".close_button", function () {
alert ("oi");
$("#spot1").addClass("spot");
$("#spot1").removeClass("grown");
});
$(document).on("click", ".clickable", function () {
if ($(this).hasClass("spot")){
$(this).addClass("grown");
$(this).removeClass("spot");
}
});
});
strangely the close_button function still won't add 'spot' or remove 'grown' though it will add any other classes and it will remove other classes... I added the if clause because I thought perhaps both function were being triggered at the same time, undoing each other, but it seems to make no difference
What happens is that your close button is placed inside your
.clickable
div, so the click event will be triggered in both elements.The event bubbling will make the click event propagate from the child nodes to their parents. So your
.close_button
callback will be executed first, and when.clickable
is reached, it will toggle the classes again. As this run very fast you can't notice the two events happened.To prevent your event from reaching
.clickable
, you need to add the event parameter to your callback function and then call thestopPropagation
method on it.Fiddle: http://jsfiddle.net/u4GCk/1/
More info about event order in general: http://www.quirksmode.org/js/events_order.html (that's where I picked that pretty ASCII art =])
I think that the problem is in the nesting of the elements. Once you attach an event to the outer element the clicks on the inner elements are actually firing the same click event for the outer element. So, you actually never go to the second state. What you can do is to check the clicked element. And if it is the close button then to avoid the class changing. Here is my solution:
In addition I'm adding and removing event handlers.
JSFiddle -> http://jsfiddle.net/zmw9E/1/
The issue is caused because of event bubbling. The first part of your code to add
.grown
works fine.The second part "removing grown class" on clicking the link doesn't work as expected as both the handler for
.close_button
and.clickable
are executed. So it removes and readd thegrown
class to thediv
.You can avoid this by using
e.stopPropagation()
inside.close_button
click handler to avoid the event from bubbling.DEMO: http://jsfiddle.net/vL8DP/
Full Code
I would recomend to cache the jQuery objects you use more than once. For Instance:
would be:
its actually more code, BUT it is muuuuuuch faster because you dont have to "walk" through the whole jQuery library in order to get the $(this) object.
Whenever I see
addClass
andremoveClass
I think why not just usetoggleClass
. In this case we can remove the.clickable
class to avoid event bubbling, and to avoid the event from being fired on everything we click inside of the.clickable
div
.I also recommend a parent wrapper for your
.clickable
divs
instead of using thedocument
. I am not sure how you are adding them dynamically so didn't want to assume your layout for you.http://jsfiddle.net/bplumb/ECQg5/2/
Happy Coding :)
Use .on()
you need event delegation as these classes are not present on DOM when DOM is ready.