Giter VIP home page Giter VIP logo

el.js's Introduction

el.js

Simple Javascript library used to create elements, no other libraries required

Examples:

Creation of elements

Create a simple element without attributes

el('div')
=> <div></div>

Some vanity methods/aliases to help you create elements quickly

# Vanity helpers
el.div()
el.p()
el.input()
el.img({'src':'http://placekitten.com/200/300'})
el.a({'src':'http://placekitten.com/200/300'})

For backwards compatibility you can still access el() via the older create methods

el.create()
el.c()

Lets add some content on creation of elements

There are a few ways to create elements with content inside. The easiest way is to pass it in via the second parameter

el('div', 'Some content')
=> <div>Some content</div>

If you pass in a html node as the second attribute, it'll be added as a child

el('ul',
  el('li', 'item 1')
)
=> <ul><li>item 1</li></ul>

If you want to create a set of li's then you can pass in an array

el('ul', [
  el('li', 'item 1'),
  el('li', 'item 2'),
  el('li', 'item 3'),
  el('li', 'item 4'),
  el('li', 'item 5'),
  el('li', 'item 6')
  ]
)
=> <ul><li>item 1</li><li>item 2</li><li>item 3</li><li>item 4</li><li>item 5</li><li>item 6</li></ul>

For backwards compatibility you can still add content via 'content'

el('li', {'content':'item 1'})

Add attributes to your elements

You could use selectors in the initial name to quick create with id's and classes

el('div#myId.content')
=> <div id="myId" class="content"></div>

el('div.row')
=> <div class="row"></div>

el also allows a key:value json object to be passed through as the second attribute

el('div.row', {'id':'myId', 'class':'my_class'})
=> <div id="myId" class="my_class row"></div>

el('a', {'class':'content', 'href':'https://github.com/markgandolfo/el.js', } 'content' )
=> <a class="content" href="https://github.com/markgandolfo/el.js">el.js</a>

Create data-attributes (or any other attributes)

el('div', {'data-action':'submit', 'id':'myId'})
=> <div data-action="submit" id="myId"></div>

Callbacks:

You can now pass a function in as a callback.

el('div', function(done) {
  console.log('this will be called after the creation of the div');
})

Examples of creating elements with id, classes, attributes, content and callbacks

el('ul.navbar', {'data-name':'navigation'}, [
  el('li', 'item 1'), 
  el('li', 'item 1'), 
  function(element) {
    this.addEventListener('click', function() {
      console.log('clicking on the navigation');
    })
  }]
)

License

See the license file

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write tests
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push -u origin my-new-feature)
  6. Create new Pull Request

el.js's People

Contributors

markgandolfo avatar thesebas avatar twentyrogersc avatar starefossen avatar modsognir avatar ocannings avatar doctorpetervannostrand avatar

Stargazers

 avatar Prashant Agarwal avatar Flavius Matis avatar J. Luca avatar Justin Williamson avatar Dmitrii Vasilev avatar Anatoliy Arkhipov avatar  avatar Angus H. avatar Korey Hinton avatar Marco Emrich avatar Kwaku Yeboah-Antwi avatar Sam IT avatar Marius Rugan avatar hamlet avatar Tamim Bin Ibrahim avatar Dingfeng Quek avatar Jonathan Dumaine avatar Andy avatar Paul avatar Benjamin Hoad avatar Simon Hambly avatar Médi-Rémi Hashim avatar Eriks Vitolins avatar  avatar July avatar Mohammed Irfan avatar Ivan Drinchev avatar  avatar Francois Lebel avatar Anthony Young avatar Ryan avatar timb avatar april avatar Marco Pedraza avatar Leandro Araújo avatar Laurens Debackere avatar Masaomi Ishikawa avatar Somsak Arnon avatar Jacob Groß avatar John avatar Ant Rodgers avatar gk avatar Vlad Shilov avatar Rob avatar Richie avatar Jackson F. de A. Mafra avatar Mustafa ismail avatar  avatar Cristian Ferrari avatar Takeshi Sonoda avatar Masaki Kawaguchi avatar tricarte avatar Gerardo Perez  avatar Michael Reyes avatar Alexander Salas Bastidas avatar Mark Steve Samson avatar  avatar André Haveman avatar HIRAKI Satoru avatar Zander Martineau avatar Kostas Bariotis avatar Spenser Isdahl avatar Michael G. Rexroad avatar  avatar  avatar Rafael de Elvira avatar Sander Houttekier avatar Syu Kato avatar Juan del Río Pacheco avatar  avatar Kevin Morio avatar Joshua Stearns avatar  avatar Blazej Gruszka avatar April Johnson avatar Nikolai Belikov avatar  avatar Rohit Trivedi avatar Jacob Kelley avatar Kohei Otsuka avatar Eric Skogen avatar liang feng avatar Yu Ao avatar  avatar Murilo Santana avatar Andrés Gottlieb avatar Stefaney Roberts avatar 小子欠扁 avatar Carl Furrow avatar Bruno Padilha avatar Toshiyuki Takahashi avatar Harvey avatar 代码家 avatar Petr Brzek avatar Aaron Kolker avatar  avatar  avatar Justin Pumford avatar Adrien F avatar

Watchers

Hemant Kumar avatar Ivan Willig avatar  avatar Leandro Araújo avatar James Cloos avatar Usman avatar  avatar Kohei Otsuka avatar Victor Navarrete avatar  avatar

el.js's Issues

Don't write out all the explicit shorthands.

You can create all the .a, .div etc method like this:

var tags = ['img', 'a', 'div', 'p', 'input'];

for(var i in tags) {
  // A new function to create local binding to the 'tag' variable.
  (function(tag) {
    el[tag] = function(attrs) {
      return this.create(tag, attrs);
   }
  })(tags[i]);
}

move "content" from attrs hash to separate arg, make it array aware

I would suggest to move content from args hash to third argument of el.create because current syntax do not allows creating such thing:

<div content="some attr">some content</div>

and after change you could do

el.create('div', {content:"some attr"}, "some content");

or even allow content to be array:

el.create(
 "ul", 
 {"content": "some attr"}, 
 [el.create("li", null, "first"), el.create("li", null, "second")]
);

to make

<ul content="some attr">
 <li>first</li>
 <li>second</li>
</ul>

use a callback or text for the content

so that writing

el.c('div', function(father) {
  father.c('a', { href: 'http://awesome.website' }, 'awesome content');
});

will generate

<div>
  <a href="http://awesome.website">awesome content</a>
</div>

[Zikes]: check paramaters and treat objects, arrays, strings differently to allow easier learning of the language.

What I'd really like to see is a system that just loops through all arguments beyond the first and handles them accordingly. If it's an object, assume it's element properties and apply them. If it's an array, assume it's child elements. If it's a function, use it as a callback. If it's a string, create a text node and append it. If it's a bare element, append it. That would allow for the greatest flexibility, I think, and make it easier to learn the library since there is no argument order to remember.

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.