Inserting an Element then Cloning it using jQuery

By Sam Deering

This is an example of using the jQuery.clone() function which some of you might not have heard of. It’s pretty simple to use and basically just copies the element you specify. In this example it creates 2 identical sidebars one on left and one on right saying “WEBPAGE-PREVIEW”. Which could be handy for showing clients that the webpage is only a preview. Changing CSS with jQuery is pretty easy to do! :)

Webpage Preview Example

The code

Just run the code in Firebug on jQuery4u homepage to see it in action.

(function ($) {
	var containerHTML = '
W E B P A G E - P R E V I E W
'; $('body').prepend(containerHTML); $('.preview').css( { 'background-color': '#DDDDDD','position': 'fixed','font-family': 'times new roman','text-wrap': 'suppress','padding': '10px','font-size': '20px','width': '20px','height': '100%','z-index': '1000','font-weight': 'bold','opacity': '0.7','vertical-align': 'middle','border': '5px solid red' }); /* clone to right */ $('.preview').clone().css({'position': 'fixed', 'right': '0'}).prependTo('body'); })(jQuery);

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.