jQuery-like handy DOM manipulation library composed from small modules.
Example:
vardom=require('domquery')dom('ul.songs:last-child').add('<li><a href="/play/{id}">Play: {title}</a></li>',{id: 123,title: "foo"}).show()$ npm install domqueryRecommended to use browserify for bundling for client-side. Sorry, it does not work in Node.
vardom=require('domquery')dom('body .foo .bar')// => [array, of, elements]dom('.foo','.bar','.qux').select('*')// [all children of .foo, .bar, .qux]dom('.foo','.bar','.qux').parent()// [parent elements of .foo, .bar, .qux]dom('.foo','.bar','.qux').siblings('button.tweet')// [all siblings that matches "button.tweet"]Details: dom-select, siblings, closest
vardom=require('domquery')dom('body .foo .bar').style('background-color','red')// OR.style({'padding': '10px','margin': '10px'})Other available Methods:
- show
- hide
Details: dom-style
domquery embeds dom-tree to provide following methods;
Insert an element to a parent element.
vardom=require('domquery')dom('<h1>{title}</h1><div>{content}',{title: 'Hello!',content: 'world'}).insert('body')Add a new element to specified parent element.
dom('body > ul').add('<li>Hello</li>')Or;
varrow=dom('<li>{0}: {1}</li>',123,'Hello World')dom('body > ul').add(row)childcan be an element, array, selector or HTML.
Adds child before reference
dom('ul.songs').addBefore('<li>third song</li>','ul.songs li:nth-child(3)')childcan be an element, array, selector or HTML.referencecan be an element, array or selector.
Adds child after reference
dom('ul.songs').addAfter('<li>third song</li>','ul.songs li:nth-child(2)')childcan be an element, array, selector or HTML.referencecan be an element, array or selector.
Replaces target with replacement
dom('ul.songs').replace('li:first-child',document.createElement('textarea'))or:
dom('ul.songs').replace('li:first-child','<li>{0}: {name}</li>',123,'new song')dom('ul .songs').remove('li:first-child')Methods: addClass, hasClass, removeClass, toggleClass
Example:
vardom=require('domquery')dom('body').addClass('foobar')dom('body').hasClass('foobar')// => truedom('body').removeClass('foobar')dom('body').hasClass('foobar')// => falsedom('body').toggleClass('foobar')dom('body').hasClass('foobar')// => trueOther Available Methods:
- addClass
- hasClass
- removeClass
- toggleClass
Details: dom-classes
domquery embeds dom-event, key-event and delegate-dom modules to provide following methods;
Add a new event
vardom=require('domquery')dom('body').on('click',function(event){console.log('clicked body')})Shortcuts:
dom('ul li').click(function(event){console.log('clicked a "li"')})- change
- click
- keydown
- keyup
- keypress
- mousedown
- mouseover
- mouseup
- resize
Remove the event listener;
dom('body').off('click',fn)Delegate event handler function for selector:
dom('body ul').on('click','li',function(event){console.log('clicked a list item!')})Adds a keyboard event:
dom('input').onKey('alt a',function(event){console.log('user pressed alt + a')})Removes a keyboard event:
dom('input').onKey('alt a',altA)dom('input').offKey('alt a',altA)functionaltA(event){console.log('user pressed alt + a')}vardom=require('domquery')dom('a.my-link').attr('href')// => http://foobar.comdom('a').attr('href','http://foobar.com')Reading:
dom('.foo').html()// equivalent of `innerHTML`dom('input.my-input').val()// equivalent of `value`Setting:
dom('.foo').html('<div>new content</div>')dom('input.my-input').val('new value')More info about it is at dom-value
