thinkbuildblog

Using the jQuery clone function

Posted by Khalid Saleem

JQuery has many great functions to make a site come alive. One of my favorite functions is .clone() -   it does exactly what its name describes, creating a copy of a single element or set of elements in the DOM and displays it where you want it on the page.  It works great for things like tool tips and more complex hover states.

Here’s an example of how to put it to good use.

Let’s say you have a list style and each “li” has an image and its description. You also have a larger version of the image in the same “li” which is given the property “display:none.” When a user clicks on “view larger image” you want to copy that image and have it appear in a separate div elsewhere, but using positioning have it line up right next to the li as shown in figure 1.

Here would be your product listening html structure <div>:

Separate the div and place it in the footer or anywhere you would like to add.

Since we have the complete HTML structure let’s move forward and create a jQuery script which will copy the image from the li (in this case, with the) and paste it in the div with the class “bubble-box.”

// Positioning on same location as real item target is.
// store the height of a target element
t_height =

// store top position
t_top =

// store the height and top position.

// store left position
t_left =

//show the bubble-box with the data and right next to the li.

“position”:

“top”:

“left”:

//Hide the clone div when mouse leaves.

//This hides it and flushes all the cloned data from DOM

You can customize the values and CSS as per your needs; the sky is the limit! Enjoy.

Categories: Tech topics Tags: , , , , 1 Comment
  • Tweet This
  • Like This
Khalid Saleem
About Khalid Saleem

Khalid joined the Pixafy team in January of 2012 as a Digital Interface Developer. He has worked for major web development firms, as well as on governmental projects, offering a unique blend of experience that he brings to the Pixafy team.

One Response to Using the jQuery clone function

  1. Jones sabo says:

    I was reading some of your articles on this site, this web site is really informative! Continue posting.

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 class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">