Giter Club home page Giter Club logo

lean-tag's Introduction

Lean Tag

Utility script for creating HTMLElements.

Example usage

import tag from 'lean-tag';

const divEl = tag('div.custom-class', 'text content');

document.body.appendChild(divEl);

const ul = tag('ul', ['1', '2', '3'].map((text) => tag('li', text)));

//<ul><li>1</li><li>2</li><li>3</li></ul>
console.log(ul.outerHTML);

For more examples, see the docs page.

Parameters

Function accepts any number of parameters, that usually are strings, nodes, arrays or objects. All other parameters are converted to string.

First param as a string

If first parameter is a string, it is treated as a description of tagName and classes. No ids are supported in that string.

That first param is split by a dot, then first element of resulting array is treated as a tagName. If it's an empty string, div is created.

const div = tag('.class-name');
const section = tag('section');
const h1 = tag('h1.class-name');
const div = tag();

Array

If parameter is an array, it can contain nodes, text, or anything else that will be converted to text node.

Object

If parameter is an object, its keys will be attribute/property/event names assigned to the element, and corresponding values will be the values or event handlers.

Node

If parameter is a Node, it will be appended as a child.

String, Number, Date, anything else

Anything else than previous options will be converted to a TextNode.

lean-tag's People

Contributors

autioch avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.