Skip to content

Instantly share code, notes, and snippets.

@mschultheiss83
Created January 30, 2016 13:24
Show Gist options
  • Save mschultheiss83/bc5cb82bd768df102cf4 to your computer and use it in GitHub Desktop.
Save mschultheiss83/bc5cb82bd768df102cf4 to your computer and use it in GitHub Desktop.
custom jquery expr and fn extend functions
/**
* Created by m.schultheiss on 16.11.2015.
*/
// Add this code anywhere you want (after jQuery has been loaded).
// Edit it to add your own expressions.
(function ($) {
'use strict';
function containsI(elem, text) {
return (
elem.textContent ||
elem.innerText ||
$(elem).text() ||
""
).toLowerCase().indexOf((text || "").toLowerCase()) > -1;
}
// @see https://api.jquery.com/jquery.extend/
// @see http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/
$.extend($.fn, {
hasAttr: function (name) {
return (typeof this.attr(name) !== "undefined" && this.attr(name) !== false);
},
justText: function (justNumber) {
this.t = $(this).clone()
.children().remove()
.end().text().trim();
return justNumber ? this.t.replace(/[^0-9]/gi, "") : this.t;
}
});
$.extend($.expr[':'], {
///////////////////////////////////////////////////////////
// form elements that are submitable based on these criteria
// element is not disabled
// element has a selected or checked attribute
// element is a textarea
// element is an input of type text or hidden
//
// @usage: $(':submitable')
// @usage: $('#myForm :submitable')
'submitable': function (a) {
return !a.disabled && (a.selected || a.checked || (a.nodeName.toUpperCase() == 'TEXTAREA') ||
(a.nodeName.toUpperCase() == 'INPUT' && (a.type == 'text' || a.type == 'hidden' || a.type == 'password')));
},
///////////////////////////////////////////////////////////
// elements that have a type attribute not equal to hidden
// use if you want to select all input elements that are not hidden
// @usage: $('input:nothidden')
'notHidden': function (a) {
return a.type && a.type != 'hidden';
},
//
'containsNC': function (elem, i, match) {
return (elem.textContent ||
elem.innerText ||
$(elem).text() ||
"").toUpperCase().indexOf((match[3] || "").toUpperCase()) >= 0;
},
// An implementation of a case-insensitive contains pseudo
// made for all versions of jQuery
'containsI': function (elem, i, match) {
return containsI(elem, match[3]);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment