Dom7 - Custom DOM Library

Framework7 doesn’t use any third party library, even for DOM manipulation. It has its own custom DOM7 - DOM library that utilizes most edge and high-performance methods for DOM manipulation. You don’t need to learn something new, its usage is very simple because it has the same syntax as well known jQuery library with support of the most popular and widely used methods and jQuery-like chaining.

To start use it there is a Dom7 global window function, but we recommend to assign it to some local variable with more handy name, like $$, but not to “$” to prevent confilicts with other libraries like jQuery or Zepto:

  1. //Export DOM7 to local variable to make it easy accessable
  2. var $$ = Dom7;

Usage Example

Just everything you already know:

  1. $$('.something').on('click', function (e) {
  2. $$(this).addClass('hello').attr('title', 'world').insertAfter('.something-else');
  3. });

Available Methods

All these methods works almost in the same way and with the same arguments as in jQuery or Zepto:

  1. $$(window).trigger('resize');
Classes
.addClass(className)Add class to elements
  1. //Add “intro” class to all paragraphs
  2. $$(‘p’).addClass(‘intro’);
.removeClass(className)Remove specified class
  1. //Add “big” class from all links with “big” class
  2. $$(‘a.big’).removeClass(‘big’);
.hasClass(className)Determine whether any of the matched elements are assigned the given class:
  1. <p class=”intro”>Lorem ipsum…</p>
  1. $$(‘p’).hasClass(‘intro’); //-> true
.toggleClass(className)Remove (if class is present) or add (if not) one or more classes from each element in the set of matched elements:
  1. <!— Before —>
  2. <h1 class=”small”>This is first title</h1>
  3. <h2>This is subtitle</h2>
  1. $$(‘h1, h2’).toggleClass(‘small’);
  1. <!— After —>
  2. <h1>This is first title</h1>
  3. <h2 class=”small”>This is subtitle</h2>
Attributes and properties
.prop(propName)Get property value:
  1. var isChecked = $$(‘input’).prop(‘checked’);
.prop(propName, propValue)Set single property value:
  1. //Make all checkboxes checked
  2. $$(‘input[type=”checkbox”]’).prop(‘checked’, true);
.prop(propertiesObject)Set multiple properties:
  1. $$(‘input’).prop({
  2. checked: false,
  3. disabled: true
  4. })
.attr(attrName)Get attribute value:
  1. <a href=http://google.com">Google</a&gt;
  1. var link = $$(‘a’).attr(‘href’); //-> http://google.com
.attr(attrName, attrValue)Set single attribute value:
  1. //Set all links to google
  2. $$(‘a’).attr(‘href’, http://google.com‘);
.attr(attributesObject)Set multiple attributes:
  1. $$(‘a’).attr({
  2. id: new-id’,
  3. title: Link to Google’,
  4. href: http://google.com
  5. })
.removeAttr(attrName)Remove specified attribute:
  1. //Remove “src” attribute from all images
  2. $$(‘img’).removeAttr(‘src’);
.val()Get the current value of the first element in the set of matched elements
  1. <input id=”myInput” type=”text” value=”Lorem ipsum/>
  1. var inputVal = $$(‘#myInput’).val(); //-> ‘Lorem ipsum’
.val(newValue)Set the value of every matched element
  1. $$(‘input#myInput’).val(‘New value here’);
Data storage
.data(key, value)Store arbitrary data associated with the matched elements
  1. $$(‘a’).data(‘user’, {
  2. id: 123’,
  3. name: John’,
  4. email: john@doe.com
  5. });
.data(key)Return the value at the named data store for the first element in the collection, as set by data(key, value) or by an HTML5 data- attribute
  1. var user = $$(‘a’).data(‘user’);
  2. //-> {id: ‘123’, name: ‘John’, email: ‘john@doe.com’}

or

  1. <p data-id=”123”>Lorem ipsum…</p>
  1. var id = $$(‘p’).data(‘id’); //-> 123
.removeData(key)Remove specified data
  1. $$(‘a’).removeData(‘user’)
Data Set
.dataset()Returns element’s data set (set of data- attributes) as plain Object
  1. <div id=”my-div” data-loop=”true” data-animate-pages=”false” data-index=”0” data-hello=”world”>
  2. </div>
  1. var dataset = $$(‘#my-div’).dataset();
  2. /
  3. dataset will contain plain object with camelCase keys and with formatted boolean and number types:
  4. {
  5. loop: true,
  6. animatePages: false,
  7. index: 0,
  8. hello: world
  9. }
  10. /
CSS transform, transitions
.transform(CSSTransformString)Adds prefixed CSS transform property:
  1. $$(‘a’).transform(‘rotate(90deg)’)
.transition(transitionDuration)Set CSS transition-duration property to collection:
  1. $$(‘p’).transition(300)
Events
.on(eventName, handler, useCapture)Add event handler function to one or more events to the selected elements.
  1. $$(‘a’).on(‘click’, function (e) {
  2. console.log(‘clicked’);
  3. });
  1. $$(‘input[type=”text”]’).on(‘keyup keydown change’, function (e) {
  2. console.log(‘input value changed’);
  3. });
.on(eventName, delegatedTarget, handler, useCapture)Live/delegated event handler
  1. $$(document).on(‘click’, a’, function (e) {
  2. console.log(‘link clicked’);
  3. });
.once(eventName, handler, useCapture)Add event handler function to one or more events to the selected elements that will be executed only once
  1. $$(‘a’).once(‘click’, function (e) {
  2. console.log(‘clicked’);
  3. });
  1. $$(‘input[type=”text”]’).once(‘keyup keydown change’, function (e) {
  2. console.log(‘input value changed’);
  3. });
.once(eventName, delegatedTarget, handler, useCapture)Live/delegated event handler that will be executed only once
  1. $$(document).once(‘click’, a’, function (e) {
  2. console.log(‘link clicked’);
  3. });
.off(eventName, handler, useCapture)Remove event handler
  1. function clickHandler(){
  2. console.log(‘clicked’);
  3. }
  4. // Add event listener
  5. $$(‘a’).on(‘click’, clickHandler);
  6. // Remove event listener
  7. $$(‘a’).off(‘click’, clickHandler);
.off(eventName, delegatedTarget, handler, useCapture)Remove live/delegated event handler
  1. function clickHandler(){
  2. console.log(‘clicked’);
  3. }
  4. // Add event listener
  5. $$(document).on(‘click’, a’, clickHandler);
  6. // Remove event listener
  7. $$(document).off(‘click’, a’, clickHandler);
.trigger(eventName, eventData)Execute all handlers added to the matched elements for the specified event
.transitionEnd(callback, permanent)Adds prefixed transitionEnd event handler to collection:
  1. $$(‘a’).transitionEnd(function(){ / do something / })
.animationEnd(callback)Adds prefixed animationEnd event handler to collection:
  1. $$(‘a’).animationEnd(function(){ / do something */ })
Styles
.width()Get the current computed width for the first element in the set of matched elements
  1. var boxWidth = $$(‘div#box’).width();
.outerWidth([includeMargin])Get the current computed width for the first element in the set of matched elements, including padding and border, and margin (if includeMargin is true)
  1. var outerWidth = $$(‘div#box’).outerWidth(true);
.height()Get the current computed height for the first element in the set of matched elements
  1. var boxHeight = $$(‘div#box’).height();
.outerHeight([includeMargin])Get the current computed height for the first element in the set of matched elements, including padding and border, and margin (if includeMargin is true)
  1. var outerHeight = $$(‘div#box’).outerHeight(true);
.offset()Get the current coordinates of the first element relative to the document
  1. var coords = $$(‘.content’).offset(); //-> {top: 100, left: 200}
  2. var top = coords.top; //-> 100
  3. var left = coords.left; //-> 200
.hide()Set “display:none” to the matched elements
  1. //hide all paragraphs
  2. $$(‘p’).hide();
.show()Set “display:block” to the matched elements
  1. //show all paragraphs
  2. $$(‘p’).show();
.css(property)Get value of specified CSS property for the first element:
  1. $$(‘.content’).css(‘left’); //-> 200px
.css(property, value)Set specified CSS property to the matched elements:
  1. $$(‘.content’).css(‘left’, 100px’);
.css(propertiesObject)Set multiple CSS properties to the matched elements:
  1. $$(‘a’).css({
  2. left: 100px’,
  3. top: 200px’,
  4. color: red’,
  5. width: 300px’,
  6. marginLeft: 17px’,
  7. padding-right’: 20px
  8. });
Scroll
.scrollTop()Get scrollTop position of element
.scrollTop(position, duration, callback)Set scrollTop “position” with animation during “duration” (in ms). Scroll top position will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed
.scrollLeft()Get scrollLeft position of element
.scrollLeft(position, duration, callback)Set scrollLeft “position” with animation during “duration” (in ms). Scroll left postion will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed
.scrollTo(left, top, duration, callback)Set scroll left and scroll top with animation during “duration” (in ms). Scroll postion will be set immediately if duration is not specified. If you have specified “callback” function, then it will be executed after scrolling completed
Dom Manipulation
.add(elements)Create a new Dom7 collection with elements added to the set of matched elements:
  1. var links = $$(‘a’);
  2. var divs = $$(‘div’);
  3. links.add(‘p’).addClass(‘blue’);
  4. links.add(divs).addClass(‘red’);
.each(callback)Iterate over collection, executing a callback function for each matched element
.html()Get the HTML contents of the first element in the set of matched elements
.html(newInnerHTML)Set the HTML contents of every matched element
.text()Get the text contents of the first element in the set of matched elements
.text(newTextContent)Set the text contents of every matched element
.is(CSSSelector)Check the current matched set of elements against CSS selector
.is(HTMLElement)Check the current matched set of elements against HTML element or Dom7 collection
.index()Return the position of the first element within the Dom7 collection relative to its sibling elements
.eq(index)Reduce the set of matched elements to the one at the specified index
.append(HTMLString)Insert content, specified by the parameter, to the end of each element in the set of matched elements
.append(HTMLElement)Insert specified HTML element to the end of element in the set of matched elements
.appendTo(HTMLElement)Insert content/elements, to the end of element specified in parameter
.prepend(newHTML)Insert content, specified by the parameter, to the beginning of each element in the set of matched elements
.prepend(HTMLElement)Insert specified HTML element to the beginning of element in the set of matched elements
.prependTo(HTMLElement)Insert content/elements, to the beginning of element specified in parameter
.insertBefore(target)Insert every element in the set of matched elements before the target. Target could be specified as CSS selector or HTML element or Dom7 collection
.insertAfter(target)Insert every element in the set of matched elements after the target. Target could be specified as CSS selector or HTML element or Dom7 collection
.next([selector])Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector
.nextAll([selector])Get all following siblings of each element in the set of matched elements, optionally filtered by a selector
.prev([selector])Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector
.prevAll([selector])Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector
.siblings([selector])Get the siblings of each element in the set of matched elements, optionally filtered by a selector
.parent([selector])Get the first parent of each element in the current set of matched elements, optionally filtered by a selector
.parents([selector])Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector
.closest([selector])For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree
.find(selector)Get the descendants of each element in the current set of matched elements, filtered by a selector
.children(selector)Get the children of each element in the set of matched elements, optionally filtered by a selector
.filter(callback)Filter collection of elements
  1. var redLinks = $$(‘a’).filter(function(index, el) {
  2. return $$(this).hasClass(‘red’);
  3. })
.remove()Remove/detach matched elements from the Dom
.empty()Remove all child nodes of the set of matched elements from the DOM. Alias for .html(‘’)
Shortcuts
.click()Trigger “click” event on collection
.click(handler)Add “click” event handler to collection
.blur()Trigger “blur” event on collection
.blur(handler)Add “blur” event handler to collection
.focus()Trigger “focus” event on collection
.focus(handler)Add “focus” event handler to collection
.focusin()Trigger “focusin” event on collection
.focusin(handler)Add “focusin” event handler to collection
.focusout()Trigger “focusout” event on collection
.focusout(handler)Add “focusout” event handler to collection
.keyup()Trigger “keyup” event on collection
.keyup(handler)Add “keyup” event handler to collection
.keydown()Trigger “keydown” event on collection
.keydown(handler)Add “keydown” event handler to collection
.keypress()Trigger “keypress” event on collection
.keypress(handler)Add “keypress” event handler to collection
.submit()Trigger “submit” event on collection
.submit(handler)Add “submit” event handler to collection
.change()Trigger “change” event on collection
.change(handler)Add “change” event handler to collection
.mousedown()Trigger “mousedown” event on collection
.mousedown(handler)Add “mousedown” event handler to collection
.mousemove()Trigger “mousemove” event on collection
.mousemove(handler)Add “mousemove” event handler to collection
.mouseup()Trigger “mouseup” event on collection
.mouseup(handler)Add “mouseup” event handler to collection
.mouseenter()Trigger “mouseenter” event on collection
.mouseenter(handler)Add “mouseenter” event handler to collection
.mouseleave()Trigger “mouseleave” event on collection
.mouseleave(handler)Add “mouseleave” event handler to collection
.mouseout()Trigger “mouseout” event on collection
.mouseout(handler)Add “mouseout” event handler to collection
.mouseover()Trigger “mouseover” event on collection
.mouseover(handler)Add “mouseover” event handler to collection
.touchstart()Trigger “touchstart” event on collection
.touchstart(handler)Add “touchstart” event handler to collection
.touchend()Trigger “touchend” event on collection
.touchend(handler)Add “touchend” event handler to collection
.touchmove()Trigger “touchmove” event on collection
.touchmove(handler)Add “touchmove” event handler to collection
.resize(handler)Add “resize” event handler to collection
.scroll(handler)Add “scroll” event handler to collection

Animation

.animate(properties, parameters)- Perform a custom animation of a set of CSS properties

  • properties - object - CSS properties to animate
  • parameters - object - animation parameters

returns Dom7 collection

  1. $$('#animate-me').animate(
  2. /* CSS properties to animate, e.g.: */
  3. {
  4. 'margin-left': 100,
  5. 'width': 200,
  6. 'height': 300,
  7. 'opacity': 0.5
  8. },
  9. /* Animation parameters */
  10. {
  11. // Animation duraion in ms, optional (default to 300)
  12. duration: 300,
  13. // Animation easing, optional (default to 'swing'), can be also 'linear'
  14. easing: 'swing',
  15. /* Callbacks */
  16. // Animation begins, optional
  17. begin: function (elements) {
  18. console.log('animation began');
  19. },
  20. // Animation completed, optional
  21. complete: function (elements) {
  22. console.log('animation completed');
  23. },
  24. // Animation in progress, optional
  25. progress: function (elements, complete, remaining, start) {
  26. /* Where
  27. complete - The call's completion percentage (as a decimal value)
  28. remaining - How much time remains until the call completes (in ms)
  29. start - The absolute time at which the call began (in ms)
  30. */
  31. console.log('animation in progress');
  32. }
  33. }
  34. );

It also supports chaining que:

  1. $$('#animate-me')
  2. .animate(
  3. {
  4. 'margin-left': 100,
  5. 'width': 200,
  6. 'height': 300,
  7. 'opacity': 0.5
  8. }
  9. )
  10. .animate(
  11. {
  12. 'width': 50,
  13. 'height': 50
  14. }
  15. );