I am trying to encode this poem using nested divs and basic jQuery. My idea was to start with one div of the class .active
that has display: block
, and all the rest div
are children of the first div
c display: none
. Now, when you click on the first one div
, it removes the class .active
from itself (adds a class to itself .static
, which at the moment just changes color) and adds .active
it to the child. In fact, the child div
becomes visible. Then you click on this child to open his child and so on until the end of the poem:
$(".active").click(function() {
$(this).removeClass("active").addClass("static");
$(this).children("div").addClass("active")
});
Only the first div class .active
exists in the DOM when loading the script. The click works on the first div and is ignored on the following, as in this fiddle:
https://jsfiddle.net/4ygfrtgb/1/
I tried event delegation as follows:
$("div").on("click", ".active", function() {
$(this).removeClass("active").addClass("static");
$(this).children("div").addClass("active");
});
However, even the first div
does not respond to a click. I understand that delegation is working on children of the elements, and I'm trying to call a class on the same element.
So, is there a way to target elements to a class that is added after loading the DOM?
Thank you.
user6946759
source
share