With JQuery, it's easy to delete existing HTML elements.
Delete element/content
To delete elements and content, you can typically use the following two JQuery methods:
- Remove ()-Deletes the selected element (and its child elements)
- Empty ()-Removes child elements from the selected element
JQuery Remove () method
The JQuery Remove () method deletes the selected element and its child elements.
Instance $ ("#div1"). Remove ();
Try it?
tip: in the real-life jquery programming section of this site, you can practice how to use jquery to delete an HTML element!
JQuery Empty () method
The JQuery Empty () method deletes the child elements of the selected element.
Instance $ ("#div1"). empty ();
Try it?
Filter for deleted elements
The JQuery Remove () method can also accept a parameter that allows you to filter the deleted element.
This parameter can be the syntax of any jQuery selector.
The following example removes all <p> elements of the class= "italic":
Instance $ ("P"). Remove (". Italic");
Try it?
JQuery Delete Element