Hi,
I have a html div container from which I want to remove certain id element using jquery remove() and use the remaining container elsewhere. I don't want to touch my original html div container, so in a way I would need to clone the original container using jquery clone().
HTML code is:
<div id="container">
<span>sample</span>
<span id="copy">sample text</span>
</div> I tried following HTML, but that didn't help:
var cont = $('#container').clone();
$(cont).remove("copy").appendTo("myclass");The complete code is:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="container">
<span>sample</span>
<span id="copy">sample text</span>
</div>
<div id="myclass"></div>
<button>Remove</button>
<script>
$("button").click(function () {
var cont = $('#container').clone();
$(cont).remove("copy").appendTo("#myclass");
});
</script>
</body>
</html>How can I achieve this?
2 years 3 weeks ago
I believe following should work. Notice I am now using id with remove in Jquery remove(). Therefore, in your code, it should be remove('#copy') and not remove('copy').
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <div id="container"> <span>sample</span> <span id="copy">sample text</span> </div> <div id="myclass"></div> <button>Remove</button> <script> $("button").click(function () { var cont = $('#container').clone(); $(cont).remove("#copy").appendTo("#myclass"); }); </script> </body> </html>