javascript - jQuery / Attach a div element inside another element and target/control the parent one -


for mobile version of website i’m using jquery create div element (close button) inside div (checkbox elements):

  var $newdiv = $("<div/>")                  .addclass("closebtn")                   .html("close [x]");   $(".checkbox_elements").prepend($newdiv);  

this works fine when add click function div inside (close button), can’t "hide" parent div (checkbox elements):

  $(".closebtn").click(function(){     $(".checkbox_elements").css("display", "none");    //or     $(this).prev(".checkbox_elements").css("display", "none");          }); 

when attach close button other element outside checkbox elements div works fine. how can click function work inside checkbox elements div container?

thanks support!

since button(closebtn) added dynamically, need try event delegation

$('.checkbox_elements').on('click', ".closebtn", function () {     $(".checkbox_elements").css("display", "none");     //or     $(this).parent(".checkbox_elements").css("display", "none"); }); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -