A simple dom helper if you want to get rid of jQuery.
You can use it with the commonjs or amd pattern otherwise it will just inject to window.domHelper
npm install domel
var domel = require( "domel" );
// general selector
domel( ".myid .subclass" ); // a list of matching elements
// get by class
domel.byClass( "my-class" ); // a list of elements
// get by id
var myEl = domel.byId( "my-id" ); // a single element
myEl.d.addClass( "foo" ).d.show()
- selector / find
- byClass
- byId
- create
- attr
- data
- last
- first
- parent
- children
- countChildren
- is
- hasClass
- addClass
- removeClass
- hide
- show
- hasId
- append
- prepend
- remove
- replaceWith
- clone
- on
- off
- emit
- domel
This module will add a d
property to every retuned dom element.
So you can use at least every domel function without adding the element el.d.{function}()
.
You can even just add this helper by calling domel.domel( el )
.
Use a selector to find dom elements
Returns: (Element|HTMLCollection) Returns all matching elements or an array of elements.
If onlyFirst
is true
only the fist element will be returned.
direct usage:
domel( selector [, context = document][, onlyFirst = false] )
wrapped usage by .d
:
el.d.find( selector[, onlyFirst = false] )
get sub elements by class. If no context
is defined document
will be used.
Returns: (Element|HTMLCollection) Returns all matching elements or an array of elements.
If onlyFirst
is true
only the fist element will be returned.
direct usage:
domel.byClass( _cl [, context = document][, onlyFirst = false] )
wrapped usage by .d
:
el.d.byClass( _cl[, onlyFirst = false] )
get an element by id
Returns: (Element) Returns the matching element
direct usage:
domel.byId( _id [, context = document] )
wrapped usage by .d
:
el.d.byId( _id )
create a new dom element
Returns: (Element) the new created element
direct usage:
domel.create( [tag="DIV"][, attributes={} ] )
get or set attributes of an element
Returns: (String|Element) Returns the attribute if no setVal
was defined. Otherwise the element self for chaining.
direct usage:
domel.attr( el, key [, setVal ] )
wrapped usage by .d
:
el.d.attr( key [, setVal ] )
get the data attributes of an element
Returns: (String|Object|Element) Returns elements data as object of the value if a key
was defined and no setVal
was defined. If the key
and setVal
are defined the the element self will be returned for chaining.
direct usage:
domel.data( el, key [, setVal ] )
wrapped usage by .d
:
el.d.data( key [, setVal ] )
the last element matching the selector
Returns: (Element) Returns the matching element
direct usage:
domel.last( el, selector )
wrapped usage by .d
:
el.d.last( selector )
the first element matching the selector
Returns: (Element) Returns the matching element
direct usage:
domel.first( el, selector )
wrapped usage by .d
:
el.d.first( selector )
the parent element or a parent up on the tree if a selector was set
Returns: (Element) Returns the parent element
direct usage:
domel.parent( el [, selector] )
wrapped usage by .d
:
el.d.parent( selector )
all direct children or all direct children matching the selector
Returns: (HTMLCollection) Returns all matching elements
direct usage:
domel.children( el, selector )
wrapped usage by .d
:
el.d.children( selector )
all direct children or all direct children matching the selector
Returns: (Number) Returns the number of children
direct usage:
domel.countChildren( el, selector )
wrapped usage by .d
:
el.d.countChildren( selector )
test the element against a selector
Returns: (Boolean) This element matches the selector
direct usage:
domel.is( el, selector )
wrapped usage by .d
:
el.d.is( selector )
check if the element has the given css class
Returns: (Boolean) This element has the class
direct usage:
domel.hasClass( el, classname )
wrapped usage by .d
:
el.d.hasClass( classname )
add a css class
Returns: (Element) Returns the element for chaining
direct usage:
domel.addClass( element, classname )
wrapped usage by .d
:
el.d.addClass( classname )
remove a css class
Returns: (Element) Returns the element for chaining
direct usage:
domel.removeClass( element, classname )
wrapped usage by .d
:
el.d.removeClass( classname )
hide the element by setting style="display:none"
Returns: (Element) Returns the element for chaining
direct usage:
domel.hide( el )
wrapped usage by .d
:
el.d.hide()
show the element by setting style="display:{display}"
Returns: (Element) Returns the element for chaining
direct usage:
domel.show( el [, display = "block"] )
wrapped usage by .d
:
el.d.show( [display = "block"] )
check if the element.id matches the given id
Returns: (Boolean) The id matches the given id
direct usage:
domel.hasId( el, id )
wrapped usage by .d
:
el.d.hasId( id )
Append html to a element
Returns: (Element) Returns the element for chaining
direct usage:
domel.append( el, html )
wrapped usage by .d
:
el.d.append( html )
Prepend html to a element
Returns: (Element) Returns the element for chaining
direct usage:
domel.prepend( el, html )
wrapped usage by .d
:
el.d.prepend( html )
Remove the element from the dom
Returns: (Element) Returns the removed element
direct usage:
domel.remove( el )
wrapped usage by .d
:
el.d.remove()
replace an element with another
Returns: (Element) Returns the replaced element
direct usage:
domel.replaceWith( el, newEl )
wrapped usage by .d
:
el.d.replaceWith( newEl )
deep clone a element
Returns: (Element) Returns the cloned node
direct usage:
domel.clone( el )
wrapped usage by .d
:
el.d.clone()
listen to a dom event.
Returns: (Element) Returns the removed element
direct usage:
domel.on( el, type, handler )
wrapped usage by .d
:
el.d.on( type, handler )
remove to dom event listener.
Returns: (Element) Returns the removed element
direct usage:
domel.off( el, type, handler )
wrapped usage by .d
:
el.d.off( type, handler )
emit an event manually
Returns: (Element) Returns the removed element
direct usage:
domel.emit( el, type )
wrapped usage by .d
:
el.d.emit( type )
add the .d
helper to an element
Returns: (Element) Returns extended object
direct usage:
domel.domel( el )
- Article: Native JavaScript Equivalents of jQuery Methods: the DOM and Forms
- Gist: Moving from jQuery
- Gist: Moving from jQuery (fork)
- Test script
- Automated Cross Browser tests
- Better docs ;-)
- IE Tests and Shims
Version | Date | Description |
---|---|---|
0.1.0 | 2015-9-17 | bugfixes; added methods: domel , create , attr , replaceWith , clone ; Changed compiler to browserify + coffeeify |
0.0.2 | 2015-9-16 | export fix |
0.0.1 | 2015-9-16 | Initial commit |
Initially Generated with generator-mpnodemodule
Name | Description |
---|---|
backlunr | A solution to bring Backbone Collections together with the browser fulltext search engine Lunr.js |
obj-schema | Simple module to validate an object by a predefined schema |
node-cache | Simple and fast NodeJS internal caching. Node internal in memory cache like memcached. |
rsmq | A really simple message queue based on redis |
rsmq-cli | a terminal client for rsmq |
rest-rsmq | REST interface for. |
nsq-logger | Nsq service to read messages from all topics listed within a list of nsqlookupd services. |
nsq-topics | Nsq helper to poll a nsqlookupd service for all it's topics and mirror it locally. |
nsq-nodes | Nsq helper to poll a nsqlookupd service for all it's nodes and mirror it locally. |
nsq-watch | Watch one or many topics for unprocessed messages. |
redis-heartbeat | Pulse a heartbeat to redis. This can be used to detach or attach servers to nginx or similar problems. |
systemhealth | Node module to run simple custom checks for your machine or it's connections. It will use redis-heartbeat to send the current state to redis. |
redis-sessions | An advanced session store for NodeJS and Redis |
connect-redis-sessions | A connect or express middleware to simply use the redis sessions. With redis sessions you can handle multiple sessions per user_id. |
redis-notifications | A redis based notification engine. It implements the rsmq-worker to safely create notifications and recurring reports. |
hyperrequest | A wrapper around hyperquest to handle the results |
task-queue-worker | A powerful tool for background processing of tasks that are run by making standard http requests |
soyer | Soyer is small lib for server side use of Google Closure Templates with node.js. |
grunt-soy-compile | Compile Goggle Closure Templates ( SOY ) templates including the handling of XLIFF language files. |
Copyright © 2015 M. Peter, http://www.tcs.de
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.