How can I either target an Appended element using Jquery or Javascript, or how can I add that appened element to the DOM? -


i've looked on internet giving same answer

     $(document).on('click', '#targetid', function() {       // stuff      }); 

instead of

 $('#targetid').click(function() {       // stuff      }); 

this nice , works fine, if have click event. within on click function, part says stuff, how can target appended element? instance append 2 divs back.

<div id="maindiv"></div>  <script>  socket.on('event', function (data)  {      $('#maindiv').append ('<div class="1st" id="'+data.id+'">one</div>     <div class="2nd" id="'+data.id+'">second</div>'); });       $(document).on('click', '.1st', function() {       //and right here to`enter       $('.2nd').css('background-color','yellow');     }  </scirpt> 

this seems not work because knowledge, element hasn't been added dom. should do? should use angular.js this?

ps i've tried adding entire appended content variable first before appending variable. , using variable.find find element within no avail. variable has context within function, null in on click function. in advance information broadens understanding of this.

the delegation of 'on' correct. once div element exists in dom, clicking should work. concern have named classname beginning number. maybe name alpha character followed number.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -