Coder Social home page Coder Social logo

do's Introduction

Do

Super small js library (one class, one static method) to easily create DOM elements.

Easy to write. Easy to read. Vanilla flavor.

Method

Do.elem(tag, options);

  • tag : string, tag name for created element, mandatory
  • options : array of properties for created element, optional, processed in this order
    • id: string, id for element
    • html: string, innerHTML for element
    • text: textNode for element (added after innerHTML)
    • attributes: array of arrays representing attributes, added with setAttribute
      • 0: attribute name, string
      • 1: attribute value, string
    • events: array of objects representing events, added with addEventListener
      • name: name of event
      • callback: function called when event is triggered
      • bindObject: bound object for callback, default is created element
      • params: parameters for callback
    • htmlClasses: array of classNames (string) for created element (added with classList.add)
    • style: css rules (string) used as style attribute for created element
    • children: array of DOM elements to appendChild to element, added with appendChild (added after innerHTML and textNode)
    • parent: DOM element used as parent for created element (created element added with appendChild)

Examples

// Create <div>Hello</div>
Do.elem('div', { text: "Hello" });
// Create <p><strong>Hello</strong> <em>everybody</em></p>
Do.elem('p', { html: "<strong>Hello</strong> <em>everybody</em>" });

// or
Do.elem('div', {
    children: [
        Do.elem('strong', {text: "Hello"}), 
        document.createTextNode(" "), 
        Do.elem('em', {text: "everybody"})
    ] 
});
// button
const button = Do.elem('button', {                       
    text : 'Send',
    parent: someDiv,
    events: [{
        name: 'click', 
        callback: this.send, 
        params: [this]
    }],
    htmlClasses: ['beautifulButton'],
    style: 'margin: 10px 20px 10px 5px' 
});
//imput 
Do.elem('input', {             
    id: 'age',
    attributes: [
        ['type', 'number'],
        ['name', 'age' ],
        ['min', '18'], 
        ['step', '1'], 
        ['placeholder', 'Fill me with yout age!']        
    ],
    parent: someForm 
});

Examples directly working in browser console

First : copy/paste Do class in console.

// set "hello" div in body
document.body.innerHTML = "";
Do.elem('div', { text: "Hello", parent: document.body });
// set "hello" button in body
document.body.innerHTML = "";
Do.elem('button', {                       
    text : 'Say hello',
    parent: document.body,
    events: [{
        name: 'click', 
        callback: function() {alert('hello');}
    }]
});
// set "hello" button in body using an Example class
document.body.innerHTML = "";

class Example {

   constructor(parent, name, buttonText) {
      this.text = "Hello";
      this.name = name;
      this.button = 
         Do.elem('button', { 
            id: "button-01",
            text: buttonText,
            parent: parent,
            events: [{
              name: 'click', 
              callback: this.displayMessage,
              params: [this]
            }],
            style: "margin: 10vmin; padding: 5px; width: 20em; background-color: #c9d8c5"
         });
    }
      
    displayMessage(example) {
        alert(`${example.text} ${example.name}\n Button id : ${this.id}`);
    }
}

new Example(document.body, "DoName", "Click me");

Improvements

It would be nice to improve Do. (More interesting options. Code optimization.) It would be nice to benchmark Do. (Compare Do versus full document.createElement etc... code.) Could be minified.

do's People

Contributors

lvd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.