admin管理员组文章数量:1322850
I want to hide parent div when a child div is clicked.I have appended the div when "add more" button is clicked.In short when "X" in div is clicked its parent div should hide.Here is my Fiddle
$( "#appenddiv" ).on( "click", function() {
$( ".uploadimg" ).append( "<div class='image'><div class='delete'>X</div></div>" );
});
$(".delete").on("click",function(){
$(this).parent().hide();
});
I want to hide parent div when a child div is clicked.I have appended the div when "add more" button is clicked.In short when "X" in div is clicked its parent div should hide.Here is my Fiddle
$( "#appenddiv" ).on( "click", function() {
$( ".uploadimg" ).append( "<div class='image'><div class='delete'>X</div></div>" );
});
$(".delete").on("click",function(){
$(this).parent().hide();
});
Share
Improve this question
asked Mar 5, 2014 at 11:13
abinayaabinaya
939 bronze badges
2
- 2 delegation is the keyword you are looking for – A. Wolff Commented Mar 5, 2014 at 11:14
-
And you can be selective too:
$(this).closest(".image").hide()
– mplungjan Commented Jul 20, 2018 at 14:37
3 Answers
Reset to default 6The reason why your code not working is, That element is being appended in to the DOM dynamically, so we have to use event delegation
to make the code work..
Try,
$(".uploadimg").on("click",".delete",function(){
$(this).parent().hide();
});
DEMO
You need event delegation for dynamically generated DOM:
$("body").on("click",'.delete',function(){
$(this).parent().hide();
});
Working Demo
you should use deligates for dynamically created items
$(document).on("click",".delete" function () {
$(this).parent().hide();
});
本文标签: javascriptJquery Hide parent div onclickStack Overflow
版权声明:本文标题:javascript - Jquery Hide parent div onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742113132a2421345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论