Q.js is a very simple, tiny and elegant DOM manipulation library that provides the essentials in an original and minimalistic way.
Just download the minified version here or include it directly in your HTML:
<script src="//cdn.rawgit.com/plugnburn/daed387b8c9046f19572/raw/6446b08ca7a78a10242b952b41eaae453d1f32c4/q.min.js"></script>
Let's look at the general syntax:
Q(selector)([iterationCallback])[.method([args])|([iterationCallback])][.method([args])|([iterationCallback])]...
The single global function, Q(selector)
, is bound to window
and Element.prototype
, so you can run it either on all elements or on a single DOM element.
After it, you can start building the call chain. The first call must always be an iteration call that runs the specified iteration callback on each selected element. If you don't need any iteration, you can just omit it (Q(selector)()
) and continue building the chain.
Each of the further chain links can be either another iteration call, or one of two predefined methods:
.val([newVal])
- ifnewVal
is passed, set a value (see the notes below) on each element matching the selector, otherwise (without parameters) get the first found element value;.on(events, cb)
- bind a live event handler to the elements matched by selector (found elements are passed asthis
to the callback, and the standardEvent
object - as the only parameter).
Let's check out some examples:
Q('h2')(function(el){el.style.borderBottom='2px solid red'})
- underlines each found h2 element with a red 2px-thick line;Q('h1')().val('My cool header')
- just sets h1 value to "My Cool header";Q('input[data-field="phone"]')(function(el){el.disabled = true}).val()
- disables all found inputs withdata-field
attribute set to "phone" and returns the first found value from them;Q('.popupBtn')().on('click touchstart', function(e){e.preventDefault();this.style.visibility='hidden';showPopup()})
- hides any.popupBtn
element on click or touchstart events and calls some external function;Q('#someId')()()()()()()()()()
- effectively does nothing but is still valid.
You can set and get values on any possible DOM element. If it's an <input>
, <select>
or <textarea>
control, its value
DOM property will be processed (additionally, if setting on a <select>
element, appropriate <option>
will be auto-selected). For any other elements that don't have value
property, their innerHTML
will be read/modified.
Thanks to the meta-construction nature, Q.js allows you to logically reference your DOM element collections. For example, instead of caching the selector like this:
var headersSel = 'h1, h2, h3, .ownHdr'
and then calling it like this:
Q(headersSel)(...)
you can cache the Q.js constructor like this:
var Headers = Q('h1, h2, h3, .ownHdr')()
and then call it like this:
Headers(...)
Much more convenient. Just a side note: if you intend to use live(constantly changing) element collections with this approach, it's important not to include final empty parentheses in the above example (as they will cause Q.js to cache the node list persistently) but instead use them when actually calling the collection for some operation. So in this case you must cache it like this:
var Headers = Q('h1, h2, h3, .ownHdr') // note the final parentheses are omitted...
and then call it like this:
Headers()(...) //... because they must be here
Simple as that.