jQuery Cookbook - Cdn.oreilly.com - O'Reilly
jQuery Cookbook - Cdn.oreilly.com - O'Reilly
jQuery Cookbook - Cdn.oreilly.com - O'Reilly
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Solution<br />
The remove() method can be used to remove a selected set of elements and their children<br />
elements from the DOM. Examine the following code:<br />
<br />
<br />
<br />
<br />
<br />
<br />
Anchors<br />
Anchor Element<br />
Anchor Element<br />
Anchor Element<br />
<br />
<br />
<strong>jQuery</strong>('a').remove();<br />
<br />
<br />
<br />
When the preceding code is loaded into a browser, the anchor elements will remain in<br />
the page until the JavaScript is executed. Once the remove() method is used to remove<br />
all anchor elements from the DOM, the page will visually contain only an element.<br />
It’s also possible to pass the method an expression to filter the set of elements to be<br />
removed. For example, our code could change to remove only anchors with a specific<br />
class:<br />
<br />
<br />
<br />
<br />
<br />
<br />
Anchors<br />
Anchor Element<br />
Anchor Element<br />
Anchor Element<br />
<br />
<br />
<strong>jQuery</strong>('a').remove('.remove');<br />
<br />
<br />
<br />
Discussion<br />
When using the <strong>jQuery</strong> remove() method, you need to keep two things in mind:<br />
1.11 Removing DOM Elements | 25