Skip to content
Extract a valid heading level (1 - 6) from a tag name
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.editorconfig
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
bower.json
browser.js
index.js
package.json
test.js

README.md

heading-level.js

NPM version Bower version Build Status Coverage Status devDependency Status

Extract a valid heading level (1 - 6) from a tag name

headingLevel('h1'); //=> 1
headingLevel('H6'); //=> 6

headingLevel('h0'); //=> null
headingLevel('h7'); //=> null
headingLevel('div'); //=> null

// ... <h1 id='blog-title'></h1> ...
const elm = document.getElementById('blog-title');
headingLevel(elm.tagName); //=> 1

Installation

Install with package manager

npm

npm install heading-level

Bower

bower install heading-level

Standalone

Download the script file directly.

API

headingLevel(tagName)

Return: Number (1 - 6) or null

It returns a Number of heading level, when the argument is a heading tag name, such as "h3" and "H4".

It returns null when the argument is not a heading tag name.

It throws an error when the argument is not a String.

const result = [];
for(let i = 0; i <= 7; i++) {
  result.push(headingLevel('h' + i));
}

result; //=> [null, 1, 2, 3, 4, 5, 6, null]
const foo = document.createElement('h1');
const bar = document.createElement('div');

headingLevel(foo.tagName); //=> 1
headingLevel(bar.tagName); //=> null

License

Copyright (c) 2014 Shinnosuke Watanabe

Licensed under the MIT License.

Something went wrong with that request. Please try again.