Skip to content

Instantly share code, notes, and snippets.

@ScotterC
Created April 25, 2013 02:54
Show Gist options
  • Save ScotterC/5457198 to your computer and use it in GitHub Desktop.
Save ScotterC/5457198 to your computer and use it in GitHub Desktop.
Vanilla javascript method to find all imgs in html, check the first and then second parent if it's a p tag and add text-align center
(function() {
var first_parent, i, img, imgs, second_parent;
imgs = document.getElementsByTagName("img");
img = void 0;
i = 0;
while (i < imgs.length) {
img = imgs[i];
first_parent = img.parentNode;
if (first_parent.localName === "p") {
first_parent.style.cssText = "text-align:center";
} else {
second_parent = first_parent.parentNode;
if (second_parent.localName === "p") {
second_parent.style.cssText = "text-align:center";
}
}
i++;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment