Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created April 27, 2012 10:52
Show Gist options
  • Select an option

  • Save sapegin/2508344 to your computer and use it in GitHub Desktop.

Select an option

Save sapegin/2508344 to your computer and use it in GitHub Desktop.
jQuery Snippets
/**
* <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);
});
});
};
/**
* 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, ''));
});
});
};
/**
* $.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