Created
November 16, 2009 13:14
-
-
Save vinilios/235962 to your computer and use it in GitHub Desktop.
jquery plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* reverseText plugin for jQuery | |
* v1.0 | |
* Reverses text within the selected nodes. | |
* | |
* By Craig Buckler, Optimalworks.net | |
* | |
* As featured on SitePoint.com | |
* Please use as you wish at your own risk. | |
*/ | |
/** | |
* Usage: | |
* | |
* From JavaScript, use: | |
* $(<select>).ReverseText({minlength: <M>, maxlength: <N>}); | |
* where: | |
* <select> is the DOM node selector, e.g. "p" | |
* <M> is the minimum length of string to reverse (optional) | |
* <N> is the maximum length of string to reverse (optional) | |
*/ | |
(function($) { | |
// jQuery plugin definition | |
$.fn.reverseText = function(params) { | |
// merge default and user parameters | |
params = $.extend( {minlength: 0, maxlength: 99999}, params); | |
// traverse all nodes | |
this.each(function() { | |
// express a single node as a jQuery object | |
var $t = $(this); | |
// find text | |
var origText = $t.text(), newText = ''; | |
// text length within defined limits? | |
if (origText.length >= params.minlength && origText.length <= params.maxlength) { | |
// reverse text | |
for (var i = origText.length-1; i >= 0; i--) newText += origText.substr(i, 1); | |
$t.text(newText); | |
} | |
}); | |
// allow jQuery chaining | |
return this; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment