From e83b13b3d81df2127143d967037d37a7f137fd7f Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 17 Jun 2017 09:51:48 +0200 Subject: [PATCH] New version of createElem --- assets/js/common/utils.js | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/assets/js/common/utils.js b/assets/js/common/utils.js index 9f985ce7..8e05d115 100644 --- a/assets/js/common/utils.js +++ b/assets/js/common/utils.js @@ -21,6 +21,51 @@ function createElem(nodeType, appendTo){ return newElem; } +/** + * Create a new HTML node (version2) + * + * @param {Object} infos Informations about the HTML node to create + * @info {String} type The type of the new node + * @info {HTMLElement} appendTo HTML Element that will receive the new node + * @info {HTMLElement} insertBefore Insert before specified HTML element + * @info {HTMLElement} class The class of the new element + * @info {HTMLElement} id The ID of the new element + * @info {HTMLElement} title The title of the new element + * @info {HTMLElement} src The src attribute of the new element + * @return {HTMLElement} The newly created element + */ +function createElem2(infos){ + + var newElem = document.createElement(infos.type); + + //Append to a specific element + if(infos.appendTo) + infos.appendTo.appendChild(newElem); + + //Append before a specific element + if(infos.insertBefore) + infos.insertBefore.parentNode.insertBefore(newElem, infos.insertBefore); + + //Specify the class of the element + if(infos.class) + newElem.className = infos.class; + + //Specify the ID of the element + if(infos.id) + newElem.id = infos.id; + + //Specify the title of the new element + if(infos.title) + newElem.title = infos.title; + + //Specify the source of the element + if(infos.src) + newElem.src = infos.src; + + //Return newly created element + return newElem; +} + /** * Get an HTML element specified by an ID *