JQuery deletes HTML elements
JQuery uses the following two methods to delete or clear an HTML element.
- Remove ()-delete a specified Element (including its child elements)
- Empty ()-clears child elements of a specified Element
For example:
JQuery Demo <script src="scripts/jquery-1.9.1.js"></script> <script> $(document).ready(function () { $("button").click(function () { $("#div1").remove(); }); }); </script> This is some text in the div. This is a paragraph in the div.
This is another paragraph in the div.
Remove div element
Empty:
JQuery Demo <script src="scripts/jquery-1.9.1.js"></script> <script> $(document).ready(function () { $("button").click(function () { $("#div1").empty(); }); }); </script> This is some text in the div. This is a paragraph in the div.
This is another paragraph in the div.
Empty the div element
JQuery's remove () method also supports a parameter that can be used to filter HTML elements to be deleted. This parameter can be any valid jQuery selector.
For example, the following code only deletesElement:
$("p").remove(".italic");