remove element jquery?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


11 points

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?



8 points

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>

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.