Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get all h1,h2,h3 etc elements in javascript?

I want to write something like this in javascript:

var all_headings = document.getElementsByTagName("h1,h2,h3,h4,h5,h6"); 

all_headings would then be a list of all elements that are h1 or h2 or h3... And in the order that they appear in the document, of course.

How do I do it?

like image 206
Eyal Avatar asked Aug 15 '11 13:08

Eyal


People also ask

How do you make H1 H2 H3?

To break it down, remember: H1 = Main keywords and subject matter, what the overall post is about. H2 = Sections to break up content, using similar keywords to the H1 tag. H3 = Subcategories to further break up the content, making it easily scannable.

How do you use H1 H2 H3 tags?

The structure of H1, H2, H3 tags Here's how Hn tags should be used: For an article or webpage, remember that the H1 title is the most important section. H2 and H3 are used to organize sub-sections, while H4, H5 and H6 are intended to provide additional information, with more details.

What is H1 H2 H3 called?

Subheaders, subheads, or header tags all refer to what's written inside of a bit of code known as H1, H2, H3. The code, placed in the text editor, tells the HTML that this is a bold subheading, and to present it visually that way.

What is H1 H2 H3 in HTML?

HTML defines six levels of headings. A heading element implies all the font changes, paragraph breaks before and after, and any white space necessary to render the heading. The heading elements are H1, H2, H3, H4, H5, and H6 with H1 being the highest (or most important) level and H6 the least.


1 Answers

With modern browsers you can do

document.querySelectorAll("h1, h2, h3, h4, h5, h6") 

Or you could get cross-browser compatibility by using jQuery:

$("h1, h2, h3, h4, h5, h6") 
like image 116
Alex Turpin Avatar answered Oct 06 '22 02:10

Alex Turpin