A small extension adding methods to Elements and Document Object to write less code and do more.
- Document.createElementWithAttributes()
Creates a new element with attributes.- params :
- tag ex: h1
- attributes (as a key/value pair) ex : {'class':'btn', 'value':'send'}
- params :
- Document.createElementWithText()
Creates a new element with text.- params :
- tag ex: p
- textContent ex: "Lorem ipsum dolor, sit amet consectetur adipisicing elit. Itaque, debitis?"
- params :
- Document.createElementWithChilds()
Creates a new element with childs.- params :
- tag ex: form
- childs (list of element) ex : [input, button]
- params :
- Document.createElementFromTemplate()
Create a new element from a template- params :
- idTemplate
- data as object
- usage
- in html file :
<template id="cardTemplate"> <div class="card"> <h1> {{ title }} </h1> <p> {{ content }} </p> </div> </template>
- in javascript file:
let element = document.createElementFromTemplate("cardTemplate", { title: "the title", content: "the content" })
- the returning element :
<div class="card"> <h1> the title </h1> <p> the content </p> </div>
- params :
Element inherits the methods provided by the Node parent class and its parent class before it : EventTarget.
- Element.setAttributes()
Sets the named attribute values for the current node.- params :
- key/value pair of attributes. ex : {'class':'btn', 'value':'send'}
- params :
- Element.appendChilds()
Adds the childs specified in argument as child to the current node.- params :
- childs (list of element) ex : [input, button]
- params :
- Element.toggleClass()
Adds or removes a class (toggle)- params :
- value of class
- params :
- Element.replaceMustachWithObjectValues()
Adds or removes a class (replace)- params :
- usage
- in html file :
<div id="card"> <h1> {{ title }} </h1> <p> {{ content }} </p> </div>
- in javascript file:
let card = _("#card"); card.replaceMustachWithObjectValues({ title: "a title", content: "A content of.." })
- result :
<div class="card"> <h1> a title </h1> <p> A content of.. </p> </div>
- usage
- params :
- _ to select element like document.querySelector
- __ to select element like document.querySelectorAll
- $() like EventTarget.addEventListener()
Registers an event handler of a specific event type to EventTarget.- params :
- target(or element)
- the event
- The object that will receive an event when an event of the specified type occurs. ex: callback function
- [option]
- params :
insert
<script src="https://unpkg.com/yombal-dom@1.0.5/utils.js" crossorigin="anonymous"></script>
in head tag
npm i yombal-dom
- insert
<script src="node_modules/yombal-dom/utils.js"></script>
in head tag before any script
git clone https://github.com/honorableCon/yombal-dom.git
- copy
utils.js
to your project directory - insert
<script src="utils.js"></script>
in head tag before any script
eNJOY !
Created by Honorable Con, M.E.R.N Stack developer
New methods suggestion or more.. You can tell me here or pull request.
This project is under the GPL V3 License
Jaajeuf! 🙏🏾