0

I have the following html content:

<div data-test='sectionA'>
    <input type="text" />
    <strong>sfdsfds</strong>
    <p>dffdfdsf</p>
</div>

I need to clone only the strong and p elements from the html content above. Here is my attempt:

First I need to identify the section:

if($("div").data("test") == "sectionA")
{
     $(this).children().not("input").each(function()
     {
           alert($(this).html().clone());
           /* firefox says clone is not a function 
              and I'm not getting <strong>dfadfa</strong>
              or the <p>sfdasdfsd</p> clone copy */

      });
}
3
  • I'm not used to jQuery, but I think that .html() returns a string. And you can only clone HTML elements, not strings. Try removing that Commented Feb 10, 2013 at 20:43
  • html function returns string not nodes/objects so you have nothing to clone. Commented Feb 10, 2013 at 20:44
  • the clone copy should have been part of the comment Commented Feb 10, 2013 at 20:44

1 Answer 1

1
var $div =  $("div[data-test=sectionA]");
var $strong = $('strong', $div).clone();
var $p = $('p', $div).clone();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.