6

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');                         
5
  • Are you perchance trying to edit the cloned html via some kind of id attribute that may no longer uniquely identify an element, now that its a clone? Commented Mar 8, 2012 at 15:40
  • sorry should have been clearer, .saveCompareShow is a div with the outter that is copied, it is there alright. I am just using the incorrect syntax to find it. The error In firebug is $htmlStr is not defined Commented Mar 8, 2012 at 15:44
  • 2
    Heads-up: you are referencing an undefined variable on the second line. Commented Mar 8, 2012 at 15:45
  • 3
    "htmlStr" and "$htmlStr" are different variables Commented Mar 8, 2012 at 15:46
  • Post the HTML and show the after state you want. Commented Mar 8, 2012 at 16:21

3 Answers 3

10

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/

Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

0

so your variable htmlStr is actually an element, not a string. but that is ok. I think you need to do this on the second line:

htmlStr.remove(".saveCompareShow");

If I see correcly, you are trying to remove elements with the class ".saveCompareShow" from the htmlStr element. Is that right?

** EDIT **

Try this to replace both line 2 and 3

$(htmlStr).clone().remove(".saveCompareShow").appendTo('.compareWrapper');  

2 Comments

I have tried that but it does not work. I see that you use htmlStr instead of my $htmlStr and that did work but it removed the class from both the original and the cloned
@KeithPower - See my edit, yes the original solution I gave will remove that element from the original element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.