JQuery

JQuery bind and clone, what’s the deal?

TAGs: 0 Comment

Posted By: eligeske on in JQuery

First off, what is clone()?

clone() allows you to make exact copy of that dom element without actually grabbing the one the you are cloning.  If you didn’t want to move a div and just wanted a copy, you would clone it.

Bind? Attaching an event handler to a specific DOM element.

If you make an Element using jQuery, like so:

// Creating a div element with jQuery
var myDiv = $("<div>", text: "CLICKEE!", { click: function(){ alert('YO!'); } });
// put it on the page
myDiv.appendTo('body');

You would see the text CLICKEE on the page, and if you clicked it you would get an alert saying YO!.
Then want to make a couple copies like this:

// clone original
var myDivClone = myDiv.clone();
// put it on the page
myDivClone.appendTo('body');

You would see again another CLICKEE on the page, but if you clicked it, nothing would happen. Why? When using the jQuery clone it does not clone the events, unless you pass a true into the parameter like so:

// clone original
 var myDivClone = myDiv.clone(true);
 // put it on the page
 myDivClone.appendTo('body');

 

Now you see the CLICKEE and the alert.

0 Responses to "JQuery bind and clone, what’s the deal?"

"SHARING IS CARING"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Learning DHTMLX Suite UI

Learn the foundation of the DHTMLX Suite quickly while building a single page application with multiple components in harmony.

Popular post