Git Product home page Git Product logo

dom.js's Introduction

This is my expermimental JavaScript micro library to convince myself that working with the DOM can be easy and we don't always need to use jQuery.

Some examples using jQuery:

var $element = $('#example');

var id = $element.attr('id');
var href = $element.attr('href');
var parent = $element.parent();
var targetUrl = $element.data('target-url');
var next = $element.next();
var html = $element.html();

$element.css('border-color', 'red');
$element.appendClass('important');
$element.toggleClass('highlight');
$element.hide();

The same functionality written without using any library:

var element = document.getElementById('example');

var id = element.id;
var href = element.href;
var parent = element.parentElement;
var targetUrl = element.dataset.targetUrl;
var next = element.nextElementSibling;
var html = element.innerHTML;

element.style.borderColor = 'red';
element.classList.add('important');
element.classList.toggle('highlight');
element.style.display = 'none';

The code above will work in modern browsers. Some polyfills will be needed to support older browsers (IE < 9), but it is currently not the aim of this library.

How to use this library?

There are no wrappers. Just some helpers. For everything else use native DOM methods like in the example above.

Selecting element(s) - these are just shortcuts:

var element, elements;

// element = document.getElementById('example');
element = dom.id('example');

// element = document.querySelector('[type=submit]')
element = dom.find('[type=submit]')

// elements = document.querySelectorAll('.buttons');
elements = dom.findAll('.buttons');

Important! The last example returns Array instead of NodeList.

You can pass element as the first argument of find and findAll methods:

var inner = dom.find(element, '.inner');
var checkboxes = dom.findAll(element, '[type=checkbox]');

If you want to work with array of elements, use forEach:

checkboxes.forEach(function(item) {
  item.checked = !item.checked; // toggle checkbox
});

Or while:

var i = checkboxes.length;
while (i--) {
  checkboxes[i].checked = false;
}

Some other helpers you can use (there are not many):

dom.remove(element); // remove element from the DOM
dom.empty(element); // remove all children from element
dom.insertAfter(element, referenceElement);
dom.insertBefore(element, referenceElement);

var form = dom.closest(element, 'form');

Events

For events use addEventListener or this shortcuts:

dom.on(element, 'click', function(e) {
  // ...
});

Event delegation can be achieved as follows:

dom.on(element, 'click', function(e) {
  if (e.target.tagName === 'A') {
    // ...
  }
});

To trigger an event simply call:

element.click();

dom.ready is also available:

dom.ready(function(e) {
  // ...
});

Effects

Sorry, this part is missing right now :(

dom.js's People

Contributors

mekto avatar

Stargazers

 avatar jia.xu avatar Ignacy Sokolowski avatar  avatar

Watchers

 avatar Ignacy Sokolowski avatar James Cloos 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.