Title: Is there a way to get a reference to all the paragraphs or headings in a web page in JavaScript?
Tags: javascript;dom
Question: I'm writing a simple Greasemonkey script to strip out all the images, headings and paragraphs from a web page (it's because someone wants to use images of several popular websites with the images and text removed in a presentation about branding of websites). I've figured out how to do so with images by using a for loop and the document.images array like this:
var i = 0;
var imglngth = document.images.length;
for(i=0;i<imglngth;i++)
document.images[i].style.display="none";
However, I don't believe there's an object representing paragraphs or headers. Any suggestions as to how I could implement this?

Here is the accepted answer: Using jQuery would be very each to get all the <p&gt; elements.
$('p').each(function(){
// do stuff
Here's a link for some info on jQuery and GreaseMonkey
Comment for this answer: Thanks, that worked OK. It's very rough and ready as I haven't really used jQuery, and I can't get it to work in Chrome, but it works perfectly in Firefox and as long as it works in one browser it should be fine.
Comment for this answer: As long as you can include the jquery script, it may. It's worth a shot?

Here is another answer: If the headers are in <h1&gt; - <h6&gt; tags, you can use the getElementsByTagName method from the DOM API, and a loop to get everything from <h1&gt; to <h6&gt;.
for( i = 1; i<= 6; i++ ) {
var tag = 'h' + i;
var heads = document.getElementsByTagName( tag );
for ( j = 0; j < heads.length; j++ ) {
heads[j].style.display = 'none';
If the document does not use header tags, you may have to reverse-engineer their stylesheet and do something with a bit more logic.
Of course, manually traversing the DOM API can be a pain. If you can use something like jQuery, it's a bit easier:
$( ':header' ).each( function() { ... } );
Comment for this answer: I would not generally use `:header` unless I had to (eg. to do `:header+:header`, which would be an unpleasant rule to express without it). As this is a non-standard selector, it forces jQuery to use the slow JavaScript Sizzle library instead of the fast `querySelectorAll` method built into modern browsers.
Comment for this answer: FYI Greasemonkey scripts can use `Array.forEach` to iterate a NodeList. Also, jQuery has the `":header"` psuedo selector for your hx tag selector there.
Comment for this answer: The jQuery selector should be `h1, h2, h3, h4, h5, h6` (note the commas)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:04/07/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><dom>', 'Title': 'Is there a way to get a reference to all the paragraphs or headings in a web page in JavaScript?', 'ViewCount': 1080, 'id': 2594611, 'user_AboutMe': '<p>Web developer. Work mostly with PHP and Javascript, love Python for my own projects.</p>\n', 'user_AccountId': 24929.0, 'user_DisplayName': 'Matthew Daly', 'user_Reputation': 8974.0, 'user_Views': 1072.0, 'user_id': 63717.0}}