Custom jQuery Selector Snippets

jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. Used by over 60% of the 10,000 most visited websites.

You can extend the library of jquery with your own plugins but custom selectors are another way to extend jQuery for your own means, and to hive off worker functionality that needn’t clutter your code.

Examples:

1. Select highest value from object or inputs fields.

$.extend($.expr[':'],
{
	highestValue: function(a,i,m,r) {
		
		dataArr = [];
		
		if( $(a).is('td') == true || $(a).is('div') == true || $(a).is('span') == true )
		{
			$.each(r, function (i){  dataArr.push(parseFloat($(this).text())); });
			maxValue = Math.max.apply(null, dataArr);
			
			return $(a).text() == maxValue ? true : false;
		}
		else if( $(a).is('input') == true )
		{
			$.each(r, function (i){  dataArr.push(parseFloat($(this).val())); });
			maxValue = Math.max.apply(null, dataArr);
			
			return $(a).val() == maxValue ? true : false;
		}
	}
});

$("#input:highestValue");
$("#div:highestValue");

2. Select lowest value from object or inputs fields.

$.extend($.expr[':'],
{
	lowestValue: function(a,i,m,r) {
			
		dataArr = [];
		
		if( $(a).is('td') == true || $(a).is('div') == true || $(a).is('span') == true )
		{
			$.each(r, function (i){  dataArr.push(parseFloat($(this).text())); });
			minValue = Math.min.apply(null, dataArr);
			
			return $(a).text() == minValue ? true : false;
		}
		else if( $(a).is('input') == true )
		{
			$.each(r, function (i){  dataArr.push(parseFloat($(this).val())); });
			minValue = Math.min.apply(null, dataArr);
			
			return $(a).val() == minValue ? true : false;
		}
	}
});

$("#input:lowestValue");
$("#div:lowestValue");

3.  Select only numerics fields

$.extend($.expr[':'],
{
	isNumeric: function(a) {
		return !isNaN(parseFloat($(a).text())) && isFinite($(a).text());
	}
});
$("#div:isNumeric");

4. Select all elements with classname icon.

$.extend($.expr[':'],
{
	icon: function(el) {
		var c = el.className.toLowerCase();
		return c.match("icon");
	}
});

$("span:icon");
Anuncios