Search code examples
javascriptjquerypositioncloneappendto

Jquery .clone()


I'm currently trying to clone with the following code:

var position = $(this).position();
var ptop = position.top;
var pleft = position.left;
$(this).click(function() {
                $(this).clone().css({
                    top: ptop,
                    left: pleft,
                    opacity: '0.55'
                })
}).appendTo(this);

I need the element to clone at the exact position than the brother element. Thats why I have:

    var position = $(this).position();
    var ptop = position.top;
    var pleft = position.left;

For getting the position. But I also what the clone to have a lighter opacity.


Solution

  • You've got your parentheses messed up. Your call to "appendTo()" is being applied after the establishment of the "click" handler.

    $(this).click(function() {
                    $(this).clone().css({
                        top: ptop,
                        left: pleft,
                        opacity: '0.55'
                    }).appendTo(this);
    });
    

    You need it inside the "click" handler, and it needs to be called on the ".clone()" return value.