Skip to content

Instantly share code, notes, and snippets.

@barmgeat
Last active October 26, 2019 05:27
Show Gist options
  • Save barmgeat/82989fd03606a37b5e0a8b22d44f0847 to your computer and use it in GitHub Desktop.
Save barmgeat/82989fd03606a37b5e0a8b22d44f0847 to your computer and use it in GitHub Desktop.
to create html elements
// 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)
}
//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