jquery to copy an element on the page

I use a hosted e-commerce solution to sell products online.  The site is pre-designed with a button that fires a JavaScript event. I'm fine with this button and the way it works.

At the bottom of each product page, there is an editable region where I can insert my own content (including code).

In this section of the page, I want a COPY of the button to appear. So I view the source code, copy this block of code:

<span style="margin-left:30px;"><a id="ctl00_content_CtlAddToCart_x12" class="pbtn btn-s btn-s-green ButtonRefreshPreview" onclick="return PreviewSubmitCrossPost(23152,295);" href="javascript:void(0);"><span><span class="pbtn-icon img-refresh"></span>Generate Proof</span></a></span>

... and paste it into the editable content region at the bottom of the page.
Doing this creates a second copy of the button.
It works fine.

The problem is, since this block of code calls a function, and the parameter of the function is the Product ID, and the product ID is different for every product ... I can't simply copy/paste this same block of code into every product page.  

Instead I have to paste the code. Then view the source code, grab the product ID, then go back into my copy and update the parameter. Because the ID is unique for each product, I have to manage these codes manually.

Using jQuery, is there a way that I can just say "Find the button on this page and make a copy of it here"?

This way I am copying/pasting the same block of code for every product, and it automatically grabs the product ID without me having to manually update it?
LVL 10
Who is Participating?
Slick812Connect With a Mentor Commented:
greetings ecarbone, , You have a couple of potential problems by copying an element (button) in javascript, one is the identical ID of two elements, that Kim Walker mentioned, and the other is pulling associated styles, functions, and placements from that element. I think what you need is  to duplicate the Click event function, on the buttons, not necessarily  the entire button HTML elements, you can simply assign click functions in javascript with an equal =

you might have your added button with a unique ID like -
<span style="margin-left:30px;">
<a id="copiedButton" class="pbtn btn-s btn-s-green ButtonRefreshPreview"  href="javascript:void(0);">
<span><span class="pbtn-icon img-refresh"></span>Generate Proof</span>

Open in new window

and no click event, , then at the bottom of the page, you have code simalr to -
document.getElementById("copiedButton").onclick = document.getElementById("ctl00_content_CtlAddToCart_x12" ).onclick;

Open in new window

Kim WalkerWeb Programmer/TechnicianCommented:
This is definitely possible. But how would you describe the element you wish to clone? In the code you've included, the outer span element does not have an id or class value to target. Is the id of the <a> tag always "ctl00_content_CtlAddToCart_x12"? If so, you could select that <a> tag, then select its parent element to clone.
ecarboneAuthor Commented:
Hi Kim,
I supposed I would have to target the id of the <a> tag. I believe it is always the same. I am new to jquery. How would I write the function to clone this element?

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

leakim971Connect With a Mentor PluritechnicianCommented:
Replace your current click event handler by this one :

// watch all click on any Anchor having ID ctl00_content_CtlAddToCart_x12
// best is to you use a class instead an ID on your button, so use the following instead :
// jQuery("body").on("click", ".george", function(evt) {
jQuery("body").on("click", "a[id=ctl00_content_CtlAddToCart_x12]", function(evt) {
    var clone = jQuery(this).closest("span").clone(); // we clone the span with the button inside
    // append the clone to the product listing

Open in new window

Kim WalkerWeb Programmer/TechnicianCommented:
One thing to beware of, the ids of the elements in the cloned version will be the same as the ids of the original elements. This could cause problems if there is other code that targets these elements. Usually, only the last element with a duplicate id will be targeted. It may be necessary to find the elements with ids in the clone and modify them before inserting the clone elsewhere on the page.
ecarboneAuthor Commented:
Sorry for the delay everyone. I agree that the duplicate "id" is not ideal. Slick, you are correct - I really want the click event, not necessarily the entire button.

I am going to try out the proposed solutions today and report back asap.

Any news ?
ecarboneAuthor Commented:
leakim and Slick both presented solutions. I ended up going with Slick's and it worked perfectly. Thank you. JavaScript is great. I need to learn more!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.