Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery-cloned div not responding to click event

The goal is to expand a div to cover the whole screen without destroying the layout.

My current solution looks basically like this:

$( ".box" ).click(function() {
    copy = $(this).clone();
    copy.addClass("box-active");
    // save .box position + size     
    // maximize div
}

$( ".box-active" ).click(function() {
    // minimize div to saved .box position + size
    $(this).remove();
}

But the cloned divs will not respond to click events. Is there a way to work around that?

Full code: http://codepen.io/deuxtan/pen/oXQpRy

like image 503
Deuxtan Avatar asked Jan 27 '26 12:01

Deuxtan


2 Answers

Use Event delegation for dynamically created class in DoM elements

$(".container").on('click', '.box-active', function() {
  if(isFullscreen){ 
    d.width = "100px";
    d.height = "100px";            
    d.top = 0;            
    d.left = 0; 

    $(this).animate(d, duration);
    isFullscreen = false;
  }
});
like image 182
Sudharsan S Avatar answered Jan 30 '26 01:01

Sudharsan S


You need to use .on for dynamically added elements.

$( ".container").on("click", ".box-active", function() {
    // ... minimize div ...
    $(this).remove();
});
like image 24
Nikhil Aggarwal Avatar answered Jan 30 '26 01:01

Nikhil Aggarwal