jQuery can remove existing contents by removing certain elements.
The remove methods are remove() and empty().
The remove() method removes the content of the selected elements and their child elements .
This example removes the <div> elements and the <ul> elements
<!DOCTYPE html> <html> <head> <script> $(document).ready(function(){ $("#btn1").click(function(){ $("#div1").remove(); }); $("#btn2").click(function(){ $("ul").remove(); }); }); </script> </head> <body> <div id="div1" style="height:250px;width:350px;border:1px solid black;background-color:cyan;"> <p> List of fresh products</p> <ul> <li>Fruits <ol> <li>Apple <li>Orange </ol> <li>Vegetable <ol> <li>Cabbage <li>Brocolli <ol> </ul> </div> <br> <button id="btn2">Remove The List</button> <button id="btn1">Remove All Content</button> </body> </html>
List of fresh products
*Refresh the page after clicking a button before you click on the other button
The remove() method can also filter elected elements with a parameter . The parameter can be a class or an id
This example removes the <h2> element with id="mytitle" and the <p> elements with class="para1".
<!DOCTYPE html> <html> <head> <script> $(document).ready(function(){ $("#btn2").click(function(){ $("h2").remove("#title"); }); $("#btn3").click(function(){ $("p").remove(".para1"); }); }); </script> </head> <body> <h2 id="mytitle">Removing Content with jQuery</h2> <p class="para1">jQuery can remove content easily.</p> <p class="para2">The methods are remove() and empty().</p> <p class="para1">You can also select content to remove.</p> <button id="btn3">Remove the Title <button id="btn4">Remove all p elements with class="para1"</button> </body> </html>
jQuery can remove content easily.
The methods are remove() and empty().
You can also select content to remove.
This example demonstrates removing elements by putting different parameters together separated by commas.
<!DOCTYPE html> <html> <head> <script> $(document).ready(function(){ $("#btn5").click(function(){ $("p").remove(".para1,.para3,#para4"); }); }); </script> </head> <body> <p class="para1">jQuery can remove content easily.</p> <p class="para2">The methods are remove() and empty().</p> <p class="para3">You can also select content to remove.</p> <p id="para4">By includng parameneters.</p> <button id="btn5">Remove all relevant p elements</button> </body> </html>
jQuery can remove content easily.
The methods are remove() and empty().
You can also select content to remove.
By including parameters.
The empty() method removes the child elements of the selected elements.
<!DOCTYPE html> <html> <head> <script> $(document).ready(function(){ $("#btn6").click(function(){ $("#div2").empty(); }); }); </script> </head> <body> <div id="div2" class="mydiv"> <h1>The empty() Method</h1> <p>Another jQuery content removing method.</p> <p>It is the empty() method.</p> <img src="https://javascript-tutor.netjQuery/ladybug.gif" style="width:30%;height:auto"> <button id="btn6">Remove the elements</button> <div> </body> </html>
Another jQuery content removing method.
It is the empty() method.
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy