0

This is my first question on stackoverflow and I hope my problem description is enough precise. I would like to add a class to some elements and then use those elements to trigger. But the following code (just an example) doesn't work. In the inspector I can see the class, but there is no reaction on click. Is there an easy solution?

$(".T").click(function(){
    $(this).addClass("current");
});

$(".current").click(function(){
    $("#Deko").hide();
});
jwssnr
  • 9
  • 3
  • 2
    [Understanding Event Delegation](https://learn.jquery.com/events/event-delegation/) – Andreas Feb 17 '16 at 14:32
  • go check this sample https://jsfiddle.net/guradio/xgqcf9f2/ – guradio Feb 17 '16 at 14:38
  • I’m not sure. the first 2 hints are to complicated for me as a beginner and @guradio is not working as a solution for my use or i don’t get it. – jwssnr Feb 17 '16 at 15:34
  • @jwssnr did you try the fiddle is that what you are after?is that what you want? – guradio Feb 17 '16 at 15:36
  • @guradio yes, but i want to ad a class to an element and after that I want to use the element with the added class for .click() – jwssnr Feb 17 '16 at 15:45
  • In your fiddle the first element allready has the class "current" – jwssnr Feb 17 '16 at 15:45
  • https://jsfiddle.net/xgqcf9f2/4/ sorry about that try this one.. i must have missed that.. the explanation is you need to delegate the event so what i did is i delegate the event on document. because during load of the DOM the element with class current is not yet created meaning it is dynamically added. to use dyamically added you need to use event delegate for more info visit here http://api.jquery.com/on/ – guradio Feb 17 '16 at 15:46
  • @guradio Thank you very much. I had to a to update your fiddle with another element for my use to test it. Now it works and now I (partly) understand this delegation thing. – jwssnr Feb 17 '16 at 20:00
  • @jwssnr glad it works now happy coding if you need any assistance and if i know i will gladly help :) – guradio Feb 18 '16 at 02:36

0 Answers0