https://github.com/sanderhelleso/javascript-functions
A list of my own handy javaScript functions
https://github.com/sanderhelleso/javascript-functions
Last synced: about 1 year ago
JSON representation
A list of my own handy javaScript functions
- Host: GitHub
- URL: https://github.com/sanderhelleso/javascript-functions
- Owner: sanderhelleso
- Created: 2018-06-07T21:40:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-08T22:13:39.000Z (about 8 years ago)
- Last Synced: 2025-02-02T13:15:57.496Z (over 1 year ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# javaScript Fuctions
A resusable function for createing specific elements and assign needed functionality to it like id, classes or events
Really handy when creating elements is a common part of the application, cleans up the main code and gives you more flexibility when it comes to DOM manipulation
```javascript
// basic example on how to use the function
const htmlElement = createElement("h1", "mainHeading", "class1 class2 class3", "I love JS", "click", "myFunction");
document.querySelector("#heading").appendChild(htmlElement);
htmlElement.click();
function createElement(type, id, classes, text, eventType, eventFunction, parent) {
// create an element with the given type
const ele = document.createElement(type);
// assign the element an ID if an ID is present
if (id != undefined) {
ele.id = id;
}
// assign the element class/classes if a class is present
if (classes != undefined) {
ele.className = classes;
}
// assign the element an innerHTML if a text is present
if (text != undefined) {
ele.innerHTML = text;
}
// assign the element an event listener if a event type and event is present
if (eventType != undefined && event != undefined) {
ele.addEventListener(eventType, eval(eventFunction));
}
// append the element to a given parent if present
if (parent != undefined) {
parent.appendChild(ele);
}
// else return the created element
else {
return ele;
}
}