-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-heading-tag.js
25 lines (21 loc) · 1.01 KB
/
dynamic-heading-tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
document.addEventListener('DOMContentLoaded', function() {
// Get all elements with the heading-tag attribute
const headers = document.querySelectorAll('[heading-tag]');
headers.forEach(header => {
// Get the heading-tag attribute value
const headerType = header.getAttribute('heading-tag');
if (headerType >= 1 && headerType <= 6) {
// Create a new heading element based on the heading-tag value
const newHeader = document.createElement('h' + headerType);
newHeader.innerHTML = header.innerHTML;
// Copy over any other attributes (excluding heading-tag)
for (let attr of header.attributes) {
if (attr.name !== 'heading-tag') {
newHeader.setAttribute(attr.name, attr.value);
}
}
// Replace the original element with the new heading element
header.parentNode.replaceChild(newHeader, header);
}
});
});