Skip to content

Instantly share code, notes, and snippets.

@mi3tek-amb
Last active December 23, 2015 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mi3tek-amb/6686214 to your computer and use it in GitHub Desktop.
Save mi3tek-amb/6686214 to your computer and use it in GitHub Desktop.
$.id = function(selector, context){
return new jQuery.prototype.init.id(selector, context);
};
$.html = function(selector, context){
return new jQuery.prototype.init.html(selector, context);
};
$.extend(
jQuery.prototype.init,
{
id : function( selector, context, rootjQuery ) {
var elem, doc;
if ( !selector ) {
return this;
}else{
elem = document.getElementById( selector );
if ( elem && elem.parentNode ) {
if ( elem.id !== selector ) {
return rootjQuery.find( selector );
}
this.length = 1;
this[0] = elem;
}
this.context = document;
this.selector = selector;
return this;
}
},
html : function( selector, context) {
if ( typeof selector === "string" ) {
if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
match = [ null, selector, null ];
} else {
match = (/^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/).exec( selector );
}
if ( match && (match[1] || !context) ) {
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context;
jQuery.merge( this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,
true
) );
if ( (/^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/).test( match[1] ) && jQuery.isPlainObject( context ) ) {
for ( match in context ) {
if ( jQuery.isFunction( this[ match ] ) ) {
this[ match ]( context[ match ] );
} else {
this.attr( match, context[ match ] );
}
}
}
return this;
}
}
};
}
}
);
jQuery.fn.init.id.prototype = jQuery.fn.init.html.prototype = jQuery.fn;
//to use
console.dir(
$.id('div')
);
console.dir(
$.html('<div>',{id:'myId'})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment