Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change html on clone

On click I am copying a div. I wish to alter some of the html and then append.

I have tried the following, it will clone and append but I cant seem to alter the html. Not sure what is the best way to handle it, i am sure I have it wrong.

thanks

var htmlStr = $(this).parents(".listingContainer").first();
$htmlStr.find(".saveCompareShow").remove()
$(htmlStr).clone().appendTo('.compareWrapper');                         
like image 502
Keith Power Avatar asked Mar 08 '12 15:03

Keith Power


People also ask

How do you replace an element with another in jQuery?

We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.

How to clone elements in jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

What is clone() in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.

How append clone in jQuery?

Copy/Paste with clone() & append() using jQuery You can copy an element from one place to another using the clone() function in jQuery. First you make a copy using the clone() function then you use the appendTo() function to indicate where you want the copy placed.


2 Answers

Once you have cloned a DOM element you should treat it just as though you wanted to change a DOM element that isn't being cloned. You just use your variable instead of a selector.

Assuming that you have HTML like this:

​<div class='listingContainer'>
    <div class='listing'>
        Something
    </div>
    <div class='listing'>
        Another something
    </div>
</div>

You can clone the contents of the listingContainer DIV and change the contents before appending them to the compareWrapper div. The following JavaScript shows a simple example:

$(document).ready(function() {
    $('.listing').click(function() {
        var $htmlStr = $(this).parents(".listingContainer").first();
        $htmlStr.clone().find('.listing').html('<li>Cloned</li>').appendTo('.compareWrapper');
    });
});

I've also posted a jsFiddle so you can see it working: http://jsfiddle.net/hkBMK/

like image 163
Wally Lawless Avatar answered Oct 14 '22 09:10

Wally Lawless


After cloning, you can treat the object just as if it was html already in the dom.

For example - html on your page:

<div id="cloneHere"></div>
<div class="well" id="buildInfoTemplate">
    <form>
        <fieldset>
          <legend></legend>
          <label></label>
          <input type="text" placeholder="Type something…">
          <span class="help-block">Example block-level help text here.</span>

          <button class="btn">Save</button>
        </fieldset>
    </form>
</div>

Lets say you wanted to change the help-block text:

var template = $('#buildInfoTemplate').clone().removeAttr('id');
$('.help-block',template).html('hi there ryan');
$('#cloneHere').append(template);

And you'll get:

<div id="cloneHere">
  <div class="well">
    <form>
        <fieldset>
          <legend></legend>
          <label></label>
          <input type="text" placeholder="Type something…">
          <span class="help-block">hi there ryan</span>

          <button class="btn">Save</button>
        </fieldset>
    </form>
  </div>
</div>
like image 29
rynop Avatar answered Oct 14 '22 08:10

rynop