Javascript DOM HTML Node removeChild Method remove element by id

Introduction

Remove a <li> element with id="myLI" from its parent element without specifying its parent node:

Click the button to remove the li element with id="myLI" from the list.

View in separate window

<!DOCTYPE html>
<html>
<body>

<ul><li>CSS</li><li id="myLI">Tea</li><li>Java</li></ul>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from   w ww.  jav  a  2s .c o  m*/
  var item = document.getElementById("myLI");
  item.parentNode.removeChild(item);
}
</script>

</body>
</html>



PreviousNext

Related