Created
November 6, 2022 07:43
-
-
Save marklchaves/1035585feb950ad1aadd7918b173d071 to your computer and use it in GitHub Desktop.
Wrap images with an a (link) tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
const images = document.querySelectorAll('img'); | |
const label = 'Images wrapped in an a tag.' | |
console.group(label); | |
images.forEach(function(elm) { | |
console.info('Wrapping this image: ', elm); | |
const wrapper = document.createElement('a'); | |
wrapper.setAttribute('href', 'https://www.caughtmyeye.cc'); | |
wrapper.appendChild(elm.cloneNode(true)); | |
if (elm.parentNode) { | |
elm.parentNode.replaceChild(wrapper, elm); | |
} | |
}); | |
console.groupEnd(label); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment