sdf-query 0.9.9
Simple utility for selecting and modifying DOM elements used by SDF CSS Framework. Lightweight alternative to some escentials of jQuery compatible with modern browser and ie11+
Live documentation and examples
Instalation
npm install sdf-query
Include the script
<script src="path_to_js/js/sdf-query.min.js"></script>
js/sdf-query.js
s(selector, limit)
Query Function
This function enables you to select html elements from the DOM and return an object which lets you modify their attributes, classes, values, styles and add event handlers.
Parameters
Name | Type | Description | |
---|---|---|---|
selector | string object |
A string which is gonna be used to query elements or a Node element. If selector starts with '#' getElementsById will be used limiting the result to 1. | |
limit | number optional |
If set to a number, will limit the results of the query to the amount. If set to one, element will be selected by using querySelector instead of querySelectorAll. |
Examples
// adds an event handler for a button of id #button_id;
// sets the attribute data-item to all the li with class '.active';
// removes class .active from all h2 with class '.active' of the page;// removes class .active from 3 of h2 of the page;
// Iterates over all the ul of a page and appends an li and prepends li;
// Custom iterator ; // Chaining ;
Returns
object
Which contains the methods for dom manipulation.
s(selector).addClass(classList)
Adds classnames to the elements in the node list
Parameters
Name | Type | Description | |
---|---|---|---|
classList | string |
List of classes separated by space or a signle classname |
Examples
// adds classes through custom iterator;
// adds classes through method
Returns
object
Query object for nesting
s(selector).after(value)
Inserts content after each element of the list. If content is a string parses the specified text as HTML and inserts the resulting nodes.
Parameters
Name | Type | Description | |
---|---|---|---|
value | string node |
String or Node to be inserted |
Examples
cheat sheet<!-- before --><element> <!-- prepend --> elements content <!-- append --></element><!-- after -->
// after a div in the div#first;
Returns
object
Query object for nesting
s(selector).append(value)
Appends content to each element of the list. If content is a string parses the specified text as HTML and inserts the resulting nodes.
Parameters
Name | Type | Description | |
---|---|---|---|
value | string node |
String or Node to be inserted |
Examples
cheat sheet<!-- before --><element> <!-- prepend --> elements content <!-- append --></element><!-- after -->
// appends a div in the div#first;
Returns
object
Query object for nesting
s(selector).attr(attr, value)
Sets the attribute of each elements in the list or, Gets the value of attribute of the first element if no arguments
Parameters
Name | Type | Description | |
---|---|---|---|
attr | string |
Attribute to be set | |
value | string |
Optional, the new attribute value |
Examples
// reads the attribute data-date from a clicked button;
Returns
mixed
Query object for nesting or value if getter
s(selector).before(value)
Inserts content before each element of the list. If content is a string, 'prepend' parses the specified text as HTML and inserts the resulting nodes.
Parameters
Name | Type | Description | |
---|---|---|---|
value | string node |
String or Node to be inserted |
Examples
cheat sheet<!-- before --><element> <!-- prepend --> elements content <!-- append --></element><!-- after -->
// inserts a div before the div#first;
Returns
object
Query object for nesting
s().create(type, html)
Creates an html element to be later appended with append
Parameters
Name | Type | Description | |
---|---|---|---|
type | string |
The type of element: div,li, button, a... | |
html | string |
Inner html of the element |
Examples
// creates a node and appends it;
Returns
object
Node element of DOM
s(selector).css(attr, value)
Sets the style of each elements in the list or, Gets the value of style of the first element if no arguments
Parameters
Name | Type | Description | |
---|---|---|---|
attr | string |
Attribute to be set | |
value | string |
Optional, the new style value |
Examples
// reads the style data-date from a clicked button;
Returns
mixed
Query object for nesting or value if getter
s(selector).each(method)
Iterates over every item from the selected element list. Sets "this" to the currently iterated element.
Parameters
Name | Type | Description | |
---|---|---|---|
method | function |
A function to execute for each node |
Examples
// Iterates over buttons with class active, gets the attribute data-state,does something and finally sets data-state to false;
Returns
object
Query object for nesting
s(selector).element()
Gets the first element in the selected list of nodes
Examples
var element = ;elementstyledisplay = 'block';;
Returns
object
First element in the list
s(selector).first()
Returns the first element in the list Alias to element()
Returns
object
Element
s(selector).find(selector)
Returns a list of decendent elements from the selected element.
Parameters
Name | Type | Description | |
---|---|---|---|
selector | string |
Returns
- Query object for nesting and dom modification
s(selector).hasClass(className)
Returns true if a class is present in the first element class list
Parameters
Name | Type | Description | |
---|---|---|---|
className | string |
Name of the class without "." |
Examples
if // to do
// checks if element is active on click, does stuff, removes class active.;
Returns
bool
If the classname is present in the list
s(selector).hide()
Hides an element. (Sets display property to none)
Examples
// hides the element;
Returns
object
Query object for nesting
s(selector).html(value)
Sets the innerHTML of each element in the list or, Gets the innerHTML of the first element on the list
Parameters
Name | Type | Description | |
---|---|---|---|
value | string |
Optional, the new innerHTML value |
Examples
// sets inner conent of body;// gets the html of the bodyvar body = ;
Returns
object
string
Query object for nesting or value if getter
s(selector).insert(position, value)
Inserts content to each element of the list. If content is a string, parses the specified text as HTML and inserts the resulting nodes.
Parameters
Name | Type | Description | |
---|---|---|---|
position | string |
Location relative to the element where to be inserted | |
value | string node |
String or Node to be inserted |
Examples
cheat sheet<!-- beforebegin --><element> <!-- afterbegin --> elements content <!-- beforeend --></element><!-- afterend -->
// inserts a div before the div#first;
Returns
object
Query object for nesting
s(selector).on(event, method)
Adds event listener to the selected elements. Sets "this" to the currently iterated element.
Parameters
Name | Type | Description | |
---|---|---|---|
event | string |
Type of the event to listen to | |
method | function |
Method to execute on the event |
Examples
;;
Returns
object
Query object for nesting
s(selector).prepend(value)
Prepends content to each element of the list. If content is a string parses the specified text as HTML and inserts the resulting nodes.
Parameters
Name | Type | Description | |
---|---|---|---|
value | string node |
String or Node to be inserted |
Examples
cheat sheet<!-- before --><element> <!-- prepend --> elements content <!-- append --></element><!-- after -->
// prepends a div in the div#first;
Returns
object
Query object for nesting
s(selector).remove()
Removes each selected element from the page
Examples
// destroys the body;
Returns
object
Query object for nesting
s(selector).removeAttr(attr)
Removes an attribute from each element in the list
Parameters
Name | Type | Description | |
---|---|---|---|
attr | string |
Name of the attribute to be removed from the element |
Examples
// removes the attribute 'data-active' from all the div with data-active="false";
Returns
object
Query object for nesting
s(selector).removeClass(classList)
Removes classes from elements in the list
Parameters
Name | Type | Description | |
---|---|---|---|
classList | string |
List of classes separated by space |
Examples
// removes the classes ".class-1, .class-2" from the first 10 elements with class .class-0 ;
Returns
object
Query object for nesting
s(selector).show()
Shows an element on the screen. (Restores original display property value)
Examples
// shows the element;
Returns
object
Query object for nesting
s(selector).text(value)
Sets the textContent of each elements in the list or Gets the value of textContent of the first element if no arguments
Parameters
Name | Type | Description | |
---|---|---|---|
value | string |
Optional, the new textContent value |
Examples
// gets the textContent of the element with id #elementvar text = text;// sets the textContent of all the first 3 li of ul#listtext'Hello, World!';
Returns
mixed
Query object for nesting or value if getter
s(selector).value(val)
Sets the value of each elements in the list or Gets the value of the first element if no arguments
Parameters
Name | Type | Description | |
---|---|---|---|
val | string |
Optional, the new value value |
Examples
// gets the value of the input with id #input_1var val = value;
Returns
object
Query object for nesting
Documentation generated with doxdox.