Last active
December 24, 2018 17:05
-
-
Save nicordev/9e680567de536248478d59c4bedcb089 to your computer and use it in GitHub Desktop.
Create a DOM element if it does not exist already
This file contains hidden or 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
/** | |
* Create a DOM element if it does not exist already | |
* | |
* @param {String} id the id of the element | |
* @param {String} eltTag HTML tag of the element | |
* @param {String} txtContent = '' the content of the element | |
* @return {DOM element} the DOM element | |
*/ | |
function createOrGetElementById(id, eltTag, txtContent = '') { | |
var elt = document.getElementById(id); | |
if (elt === null) { | |
elt = document.createElement(eltTag); | |
elt.id = id; | |
} | |
if (txtContent) | |
elt.appendChild(document.createTextNode(txtContent)); | |
return elt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment