Last active
October 26, 2019 05:27
-
-
Save barmgeat/82989fd03606a37b5e0a8b22d44f0847 to your computer and use it in GitHub Desktop.
to create html elements
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
// Ex: create category container | |
/// add the consts in data obj can be not definde just the type is requseted | |
const catContainer = htmlE({ | |
type: 'div', // type of the element | |
classes: 'first-class second-class', // css classes | |
id: elementId, // element id | |
container: container, // parent container to append to | |
onClick: catClick, // on element click function | |
params: [category] // on element click function params | |
}); | |
// Ex: on click function: how to hoanle params: | |
//on category click | |
function catClick(params){ | |
const cat = params[0]; | |
console.log(cat._id) | |
} |
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 html element: | |
function htmlE(data){ | |
const e = $('<'+ data.type +'>') | |
if(data.classes){ | |
e.addClass(data.classes); | |
} | |
if(data.id){ | |
e.attr('id', data.id); | |
} | |
if(data.html){ | |
e.html(data.html); | |
} | |
if(data.text){ | |
e.text(data.text); | |
} | |
if(data.container){ | |
e.appendTo(data.container); | |
} | |
if(data.onClick){ | |
e.click(()=>{ | |
data.onClick(data.params) | |
}); | |
} | |
return e; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment