Skip to content

Instantly share code, notes, and snippets.

@goatandsheep
Last active October 23, 2016 03:32
Show Gist options
  • Save goatandsheep/0e5943828a34f87f13ec64967ef765c1 to your computer and use it in GitHub Desktop.
Save goatandsheep/0e5943828a34f87f13ec64967ef765c1 to your computer and use it in GitHub Desktop.
Convert from one element type to another
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<link id="elToReplace" tag="img" type="image/jpeg" href="https://www.gravatar.com/avatar/6275d97cde41c8c0d2f0941caf42abf7" class="tormedia">
<h1>I am formatted with a linked style sheet</h1>
<p>Me too!</p>
</body>
<script>
var elements = document.getElementsByClassName("tormedia")
console.log(elements)
for(var i=0; i< elements.length; i++){
var elBefore = elements[i]
var attributes = elBefore.attributes
var elAfter = document.createElement("img")
var attr;
for(attr in elBefore){
if (!attributes[attr] == ""){
var atValue = attributes[attr].value
if (attr == 'href'){
attr = 'src'
}
elAfter.setAttribute(attr, atValue)
}
}
}
elBefore.parentNode.replaceChild(elAfter, elBefore)
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment