Search code examples
jqueryclonecode-duplication

How to clone an Add to cart button with jquery


how might I duplicate an "Add to cart" button to another place with jQuery?

.clone() maybe?

This is the button I want to clone

<form id="buyForm" class="hidden-print" novalidate="novalidate">
<input type="number" class="quantity" value="1" required="" min="1">
<button type="submit" data-buy-button="" data-addurl="/shoppingcart/add/5010753401" data-dependent-accessory="" data-addresponse="Dropdown" class="btn btn-success ladda-button btn-buy buy btn-lg big" data-style="zoom-out" data-add-to-cart-ticket="L3Byb2R1Y3QtcGFnZS9wcm9kdWN0LWluZm9ybWF0aW9uOyM7cHJvZHVjdF9rZXk7NTAxMDc1MzQwMTE7Izs">
<span class="ladda-label">Add to cart</span></button>
</form>    

This is the code I've created in order to duplicate or "clone" the button

$( "body" ).prepend( "<div class='stickyDiv'></div>" );
$(".stickyDiv").css({"z-index": "99", "position": "fixed", "bottom": "0", "height": "100px", "width":"100%","margin":"0", "background":"#0278c1", "opacity":"0.5"});
$( "#buyForm" ).clone().appendTo( ".stickyDiv" );

Solution

  • see this:

    var button = $( "#buyForm button" ).clone();
    $( ".stickyDiv" ).append(button);
    

    or:

    $( "body" ).prepend( "<div class='stickyDiv'>"+button[0].outerHTML+"<div>" );