Skip to content

Instantly share code, notes, and snippets.

@pattonwebz
Created March 26, 2025 19:55
Show Gist options
  • Select an option

  • Save pattonwebz/ab9126393791742dea9f67d63dcfb5f7 to your computer and use it in GitHub Desktop.

Select an option

Save pattonwebz/ab9126393791742dea9f67d63dcfb5f7 to your computer and use it in GitHub Desktop.
document.addEventListener("DOMContentLoaded", function () {
/*
* Find any insteances of the selector in oldTag and replace it with the tag from newTag.
*/
function replaceHeadingLevel(oldTag, newTag) {
document.querySelectorAll(oldTag).forEach(function (element) {
var newHeading = document.createElement(newTag);
newHeading.innerHTML = element.innerHTML;
// Copy attributes from old heading to new heading.
Array.from(element.attributes).forEach(attr => {
newHeading.setAttribute(attr.name, attr.value);
});
element.replaceWith(newHeading);
});
}
// Change all H2s inside blog posts to H3s.
replaceHeadingLevel(".post-content h2", "h3");
// Change all H4s to H2s.
replaceHeadingLevel(".post-content h4", "h2");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment