-
Notifications
You must be signed in to change notification settings - Fork 24
dom
Usage: $l.id(elementId[, parent])
var myDiv = $l.id('my-div');
console.log(myDiv);
prints: <div id="my-div"></div>
Usage: $l.idc(elementId)
var myDiv = $l.idc('my-div');
console.log(myDiv);
prints: <div id="my-div"></div>
Usage: $l(selector[, parent])
var textUsername = $l('#username');
console.log(textUsername);
prints: <input id="username" class="inp" type="text" name="username" />
Usage: $l(selector[, parent])
Note: selector argument must be an array
var textboxes = $l(['.inp']);
console.log(textboxes);
prints: [ <input id="username" class="inp" type="text" name="username" />, <input id="password" class="inp" type="password" name="password" /> ]
Usage: $l.dom.selectByClass(selector[, parent])
var textboxes = $l.dom.selectByClass('inp');
console.log(textboxes);
prints: [ <input id="username" class="inp" type="text" name="username" />, <input id="password" class="inp" type="password" name="password" /> ]
Usage: $l.dom.docprop(classname)
// if document's root element is <html class="js no-touch localstorage">
if ($l.dom.docprop('localstorage')) {
localStorage.setItem('test', 'testvalue');
}
Usage: $l.dom.attr(element, attribute[, value])
var target = $l.id('element');
// setting attribute
$l.dom.attr(target, 'id', 'new-id');
// getting attribute
console.log($l.dom.attr(target, 'id')); // prints new-id
Usage: $l.dom.data(element, key[, value])
var target = $l.id('element');
// setting data (data-tag attribute on the element)
$l.dom.data(target, 'tag', 'improvement');
// getting data
console.log($l.dom.data(target, 'tag')); // prints improvement
Usage: $l.dom.setEvent(element, eventname, fnc)
$l.dom.setEvent(
$l('#btn-submit'), // single element
'click',
function(event, element) {
console.log(event);
return false; // override event
}
);
$l.dom.setEvent(
$l(['.confirm-action']), // array of elements
'click',
function(event, element) {
if (!confirm('Are you sure to do it?')) {
return false; // cancel event
}
}
);
Usage: $l.dom.create(html)
var fragment = $l.dom.create(
'<div class="test"><strong>bold</strong> content</div>'
);
document.body.insertBefore(fragment, document.body.firstChild);
or
Usage: $l.dom.insert(element, position, content)
$l.dom.insert(
document.body.firstChild,
'beforeBegin', // beforeEnd, afterBegin, afterEnd
'<div class="test"><strong>bold</strong> content</div>'
);
Usage: $l.dom.createElement(element, attributes, children)
var element = $l.dom.createElement(
'DIV',
{ 'class': 'pull-right' },
'test'
);
$l('body').appendChild(element);
var target = $l('#content');
// clear all children and content inside the target
$l.dom.clear(target);
// insert new content
$l.dom.append(target, '<em>second</em>');
$l.dom.prepend(target, 'first');
// replace created content
$l.dom.replace(target, '<div>fresh start</div>');
// clone first child
$l.dom.clone(target.firstChild, $l.dom.cloneAppend);
Please don't hesitate to submit issues and pull requests.