jquery - click event does not works with copied HTML -



jquery - click event does not works with copied HTML -

this question has reply here:

event binding on dynamically created elements? 13 answers

click event not works copied html?

<ul class="dir">//the given ul <li> <img class="thumb" src="1.jpg" > </li> <li> <img class="thumb" src="2.jpg" > </li> <li> <img class="thumb" src="3.jpg" > </li> <li> <img class="thumb" src="4.jpg" > </li> <li> <img class="thumb" src="5.jpg" > </li> <li> <img class="thumb" src="6.jpg" > </li> <li> <img class="thumb" src="7.jpg" > </li> </ul> <ul class="copy"></ul> //copied ul <script> $(function(){ $(".copy").html($(".dir").html()); $(".thumb").click(function(){ alert("it works on .dir's thumb not on .copy's thumb .. why?"); } ); } </script>

you have utilize event-delegation

$(document).on('click', '.thumb', function(){ alert("this works every if copied"); });

for work. .click attach handler elements, existed @ point of declaring handler.

jquery html

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -