Skip to content

Instantly share code, notes, and snippets.

@jamesperrin
Last active May 18, 2023 21:15
Show Gist options
  • Save jamesperrin/2240c3823ae2123860abf31901982892 to your computer and use it in GitHub Desktop.
Save jamesperrin/2240c3823ae2123860abf31901982892 to your computer and use it in GitHub Desktop.
(function ($) {
/**
* jQuery plugin that places cursor at the end of text
* SEE: http://stackoverflow.com/questions/4609405/set-focus-after-last-character-in-text-box
* @param {Node} The input element
* @return {Node} The input element
*
*/
$.fn.focusTextToEnd = function () {
this.focus();
var $thisVal = this.val();
this.val('').val($thisVal);
return this;
}
}(jQuery));
(function ($) {
/**
* jQuery plugin that allows Numeric only control handler
* */
$.fn.forceNumericOnly = function () {
$(this).keydown(function () {
const keyCode = event.charCode || event.keyCode || event.which || event.code || 0;
const isAllowedKeyCodes = keyCode == 8
|| keyCode == 9
|| keyCode == 13
|| keyCode == 46
|| keyCode == 110
|| keyCode == 190
|| (keyCode >= 35 && keyCode <= 40)
|| (keyCode >= 48 && keyCode <= 57)
|| (keyCode >= 96 && keyCode <= 105);
const notAllowKeyCodes = event.shiftKey && (keyCode >= 48 && keyCode <= 57);
// numeric key exceptions:
// backspace, tab, delete, enter, arrows, numbers and keypad numbers, home, end, period, and numpad decimal
if (!isAllowedKeyCodes || notAllowKeyCodes) {
event.preventDefault();
return false;
}
});
}
}(jQuery));
(function ($) {
/**
* jQuery plugin that allows Numeric only control handler
* @param {Node} disable True (default) or False to Disable element
*/
$.fn.disableElement = function (disable) {
if (typeof disable === 'undefined') {
disable = true;
}
$(this).prop('disabled', disable).prop('aria-disabled', disable).prop('aria-live', 'polite');
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment