Created
April 27, 2012 10:52
-
-
Save sapegin/2508344 to your computer and use it in GitHub Desktop.
jQuery Snippets
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
| /** | |
| * <option data-label-male="Dude" data-label-female="Dudette">Dude</option> | |
| */ | |
| $.fn.changeSelectLabels = function(key) { | |
| key = 'label-' + key; | |
| return $(this).each(function() { | |
| $(this).find('option').each(function() { | |
| var option = $(this), | |
| label = option.data(key); | |
| if (label) | |
| option.text(label); | |
| }); | |
| }); | |
| }; |
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
| /** | |
| * Input filter | |
| */ | |
| var filters = { | |
| rus: "а-яёА-ЯЁ'’ ", | |
| lat: "a-zA-Z'’ " | |
| }; | |
| $.fn.filterChars = function(chars) { | |
| if (filters[chars]) chars = filters[chars]; | |
| var charRegex = new RegExp('^[' + chars + ']$'), | |
| stringRegex = new RegExp('[^' + chars + ']', 'g'); | |
| return $(this).each(function() { | |
| var elem = $(this); | |
| elem | |
| .on('keypress', function(e) { | |
| if (!charRegex.test(String.fromCharCode(e.which))) | |
| return false; | |
| }) | |
| .on('input', function(e) { | |
| elem.val(elem.val().replace(stringRegex, '')); | |
| }); | |
| }); | |
| }; |
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
| /** | |
| * $.find() that finds in root element too | |
| */ | |
| $.fn.findWithRoot(selector) { | |
| return this.filter(selector).add(this.find(selector)); | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment