Skip to content

Instantly share code, notes, and snippets.

@ideaflare
Created July 21, 2017 21:01
Show Gist options
  • Select an option

  • Save ideaflare/5cfe17e0c6f62e6706a7fc7704a84fef to your computer and use it in GitHub Desktop.

Select an option

Save ideaflare/5cfe17e0c6f62e6706a7fc7704a84fef to your computer and use it in GitHub Desktop.
Create custom element and style it.
<!DOCTYPE html>
<html>
<body>
<div id="container" style="padding: 20px; border: 1px dashed grey;">
<div>Don't replace this.</div>
<custom-button></custom-button>
<div>Don't replace this either.</div>
</div>
<script>
var custom_button_html =
' <button>' +
' Custom ' +
' </button>'
let updateTags = (newHtml, tag) => {
let elements = document.getElementsByTagName(tag.toUpperCase())
Array.from(elements).forEach((e) => {
// insert the new element just before <x>
e.insertAdjacentHTML('beforeBegin', newHtml);
// now e's previousSibling should be the newly added element
var new_elem = e.previousSibling;
// get rid of e
e.parentElement.removeChild(e);
new_elem.style.background = 'palegreen';
})
}
updateTags(custom_button_html,'custom-button');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment