Q:

Clone a specific image from a div to another div but change onClick and ID

I have code below:
function add_trade(elem)
{
    var myElement = $(elem);
    var img = myElement.clone();
    $('#chosen').append(img); 
}

<div id="trade">    
    <div id="chosen"></div>    
</div>          

<div id="page">         
    <div id="backpack">
        <img src="ex1.jpg" onclick="add_trade(this);">
        <img src="ex2.jpg" onclick="add_trade(this);">
    </div>      
</div>

This is now working and I can clone the image and place it in my "CHOSEN"-. The problem is that it copies the OnClick and I would like it replaced for another function, so they can be able to click that cloned image and remove it from the "Chosen" div. Is this possible?

A:

you can do this in another way by creating element not cloning it. try this
    function add_trade(elem)
    {
        var myElement = $(elem),
        elemSrc = element.src;

        var img = $('<img/ >', {
              src: elemSrc,
              onclick: "your function here";
           });
        $('#chosen').append(img); 
    }

or solution with your approach:
before append just change attr onclick
function add_trade(elem)
{
    var myElement = $(elem);
    var img = myElement.clone();
    $(img).attr('onclick', 'new function');

   $('#chosen').append(img); 
}

