Skip to content

Instantly share code, notes, and snippets.

@leo020588
Last active August 24, 2022 17:20
Show Gist options
  • Select an option

  • Save leo020588/7537e4f9ceff81d87d83b2ca3d5c3533 to your computer and use it in GitHub Desktop.

Select an option

Save leo020588/7537e4f9ceff81d87d83b2ca3d5c3533 to your computer and use it in GitHub Desktop.
Utility functions for DOM manipulation
/**
* https://gist.github.com/leo020588/7537e4f9ceff81d87d83b2ca3d5c3533
* Version: 1.1.0
* Updated: 2022-08-24
*/
(function ( window, document, classList, insertBefore, parentNode, replace ) {
// NODE CREATION -----------------------------------------------------------
var CreateTextNode = function ( text ) {
return document.createTextNode( text );
};
var CreateElementNode = function ( tag, attrs, props ) {
attrs = attrs || {};
props = props || {};
// class support: div.multiple.clasess
if ( /\./.test( tag ) ) {
attrs.class = tag.split( '.' ).slice( 1 ).join( ' ' );
tag = tag[replace]( /\..*/, '' );
}
// id support: div#my-div
if ( /#/.test( tag ) ) {
attrs.id = tag[replace]( /.+#/, '' );
tag = tag[replace]( /#.*/, '' );
}
var el = document.createElement( tag ), k;
for ( k in attrs ) {
el.setAttribute( k, attrs[ k ] );
}
for ( k in props ) {
el[ k ] = props[ k ];
}
return el;
};
// GLOBAL SELECTORS --------------------------------------------------------
var SelectElement = function ( selector ) {
return this.querySelector( selector );
};
var SelectElements = function ( selector ) {
return Array.from( this.querySelectorAll( selector ) );
};
Object.assign( Element.prototype, {
// SELECTORS -----------------------------------------------------------
$ : SelectElement,
$$ : SelectElements,
$children: function () {
return Array.from( this.children );
},
// ATTR MANIPULATION ---------------------------------------------------
$setAttr: function ( attr, value ) {
this.setAttribute( attr, value );
return this;
},
$remAttr: function ( attr ) {
this.removeAttribute( attr );
return this;
},
// CONTENT MANIPULATION ------------------------------------------------
$text: function ( text ) {
this.textContent = text;
return this;
},
$html: function ( html ) {
this.innerHTML = html;
return this;
},
$empty: function () {
this.textContent = '';
return this;
},
// CLASS MANIPULATION --------------------------------------------------
$addClass: function ( className ) {
this[ classList ].add( className );
return this;
},
$removeClass: function ( className ) {
this[ classList ].remove( className );
return this;
},
$toggleClass: function ( className ) {
this[ classList ].toggle( className );
return this;
},
$hasClass: function ( className ) {
return this[ classList ].contains( className );
},
// DOM MANIPULATION ----------------------------------------------------
$adopt: function () {
for ( var i = 0, l = arguments.length; i < l; i++ ) {
this.appendChild( arguments[ i ] );
}
return this;
},
$inject: function ( target, location ) {
switch ( location ) {
case 'top':
target[ insertBefore ]( this, target.firstChild );
break;
case 'before':
target[ parentNode ][ insertBefore ]( this, target );
break;
case 'after':
target[ parentNode ][ insertBefore ]( this, target.nextElementSibling );
break;
case 'bottom':
default:
target.appendChild( this );
}
return this;
},
$grab: function ( target, location ) {
target.$inject( this, location );
return this;
},
$replace: function ( target ) {
target.$grab( this, 'before' ).$dispose();
return this;
},
$dispose: function () {
if (this[ parentNode ]) {
this[ parentNode ].removeChild( this );
}
return this;
},
// EVENT HANDLING -----------------------------------------------------
$addEvent: function ( type, listener ) {
this.addEventListener( type, listener );
return this;
},
$removeEvent: function ( type, listener ) {
this.removeEventListener( type, listener );
return this;
},
// STYLE MANIPULATION --------------------------------------------------
$setStyle: function ( prop, value ) {
prop = prop[replace](
/-\w/g,
function ( str ) { return str.substr( 1 ).toUpperCase(); }
);
this.style[ prop ] = value;
return this;
},
$setStyles: function ( props ) {
for ( var k in props ) {
this.$setStyle( k, props[ k ] );
}
return this;
},
});
// EXPOSE UTILS ------------------------------------------------------------
window.$T = CreateTextNode;
window.$E = CreateElementNode;
document.$ = SelectElement;
document.$$ = SelectElements;
})( window, document, 'classList', 'insertBefore', 'parentNode', 'replace' );
// https://gist.github.com/leo020588/7537e4f9ceff81d87d83b2ca3d5c3533
!function(t,s,e,n,r,o){function i(t){return this.querySelector(t)}function u(t){return Array.from(this.querySelectorAll(t))}Object.assign(Element.prototype,{$:i,$$:u,$children:function(){return Array.from(this.children)},$setAttr:function(t,e){return this.setAttribute(t,e),this},$remAttr:function(t){return this.removeAttribute(t),this},$text:function(t){return this.textContent=t,this},$html:function(t){return this.innerHTML=t,this},$empty:function(){return this.textContent="",this},$addClass:function(t){return this[e].add(t),this},$removeClass:function(t){return this[e].remove(t),this},$toggleClass:function(t){return this[e].toggle(t),this},$hasClass:function(t){return this[e].contains(t)},$adopt:function(){for(var t=0,e=arguments.length;t<e;t++)this.appendChild(arguments[t]);return this},$inject:function(t,e){switch(e){case"top":t[n](this,t.firstChild);break;case"before":t[r][n](this,t);break;case"after":t[r][n](this,t.nextElementSibling);break;default:t.appendChild(this)}return this},$grab:function(t,e){return t.$inject(this,e),this},$replace:function(t){return t.$grab(this,"before").$dispose(),this},$dispose:function(){return this[r]&&this[r].removeChild(this),this},$addEvent:function(t,e){return this.addEventListener(t,e),this},$removeEvent:function(t,e){return this.removeEventListener(t,e),this},$setStyle:function(t,e){return t=t[o](/-\w/g,function(t){return t.substr(1).toUpperCase()}),this.style[t]=e,this},$setStyles:function(t){for(var e in t)this.$setStyle(e,t[e]);return this}}),t.$T=function(t){return s.createTextNode(t)},t.$E=function(t,e,n){e=e||{},n=n||{},/\./.test(t)&&(e.class=t.split(".").slice(1).join(" "),t=t[o](/\..*/,"")),/#/.test(t)&&(e.id=t[o](/.+#/,""),t=t[o](/#.*/,""));var r,i=s.createElement(t);for(r in e)i.setAttribute(r,e[r]);for(r in n)i[r]=n[r];return i},s.$=i,s.$$=u}(window,document,"classList","insertBefore","parentNode","replace");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment