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?
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With