How to run my jquery code on a div that pasted a jquery code form?

I need your help in my code. I have a problem when a new div is inserted from jquery the inserted div is not applicable to my jquery code, if anyone has a solution, please tell me this my HTML too

<input type="text" id="t" value=""/> <a href="#" class="btn">click</a><br/> <br/> <div class="profileInfoSectionwall"style="border:1px solid #ccc"> text111111 <div class="remove" style="display: none;"> <a class="post_remove" rel="1" herf="#">X</a> </div> </div> <br/> <div class="profileInfoSectionwall"style="border:1px solid #ccc"> text222222 <div class="remove" style="display: none;"> <a class="post_remove" rel="2" herf="#">X</a> </div> </div> 

and this jquery code

  $(document).ready(function(){ $(".profileInfoSectionwall").mouseenter(function(){ $(this).children(".remove").show(); }); $(".profileInfoSectionwall").mouseleave(function(){ $(".remove").hide(); }); $(".btn").click(function(){ var s=$("#t").attr("value"); $(this).after("<br/><br/><div class='profileInfoSectionwall' style='border:1px solid #ccc'>"+s+"<div class='remove' style='display: none;'><a class='post_remove' rel='3' herf='#'>X</a></div></div>"); return false; }) }) 

early

+4
source share
4 answers

try the .live function live in jquery

  $(".profileInfoSectionwall").live('mouseenter', function(){ $(this).children(".remove").show(); }); $(".profileInfoSectionwall").live('mouseleave', function(){ $(this).children(".remove").hide(); }); 
0
source

You might want to try using the .on method to something like:

 $(".profileInfoSectionwall").on('mouseenter', function(){ $(this).children(".remove").show(); }); $(".profileInfoSectionwall").on('mouseleave', function(){ $(".remove").hide(); }); 
+6
source

I created a fiddle http://jsfiddle.net/mXBkV/1/

 $(document).on('mouseenter', 'div.profileInfoSectionwall', function(){ $(this).children(".remove").show(); }); $(document).on('mouseleave', 'div.profileInfoSectionwall', function(){ $(".remove").hide(); }); 
+3
source

In fact, in your case, you need the live () method, since you want to handle events on existing or future DOM elements (i.e. created by JQuery).

So here you have the working code:

http://jsfiddle.net/sidou/CMab3/

let me know if you want to improve it.

+1
source

Source: https://habr.com/ru/post/1399164/


All Articles