Javascript DOM How to - Delete a div by id








Question

We would like to know how to delete a div by id.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--   w  ww.  j a v a2s  .  co m-->
    $(document.getElementById("my")).remove();
    $(document.getElementById("your")).remove();
});
</script>
</head>
<body>
  <div id="my">hello</div>
  <div id="your">there</div>
</body>
</html>

The code above is rendered as follows: