Dive Into Greasemonkey

Teaching an old web new tricks

4.9. Removing an element

You can use Greasemonkey to remove entire chunks of a page in one fell swoop, with the removeChild function.

Example: Remove an ad sidebar

This presumes that there is an element whose ID is "ads".

var adSidebar = document.getElementById('ads');
if (adSidebar) {
    adSidebar.parentNode.removeChild(adSidebar);
}
[Note]

Removing an element with removeChild will also remove all the content inside it. For example, if you remove a <table> element, this will also remove all of its table cells (<td> elements).

[Tip]

If all you want to do is remove ads, it's probably easier to install AdBlock and import an up-to-date filter list than to write your own user script.

Real examples

← Inserting content after an element
Replacing an element with new content →