Last active
May 18, 2023 21:16
-
-
Save jamesperrin/2c1db52ee5cb381ed7026769fd976a9b to your computer and use it in GitHub Desktop.
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
/* | |
* Places cursor at the end of text | |
* (c) 2019 James Perrin, MIT License, https://www.countrydawgg.com | |
* SEE: http://stackoverflow.com/questions/4609405/set-focus-after-last-character-in-text-box | |
* @param {Node} The input element | |
* @return {Node} The input element | |
*/ | |
HTMLInputElement.prototype.focusTextToEnd = function () { | |
this.focus(); | |
const thisVal = this.value; | |
this.value = ""; | |
this.value = thisVal; | |
return this; | |
}; | |
/* | |
* Places cursor at the end of text | |
* (c) 2019 James Perrin, MIT License, https://www.countrydawgg.com | |
* @param {Node} The input element | |
* @return {Node} The input element | |
*/ | |
HTMLTextAreaElement.prototype.focusTextToEnd = function () { | |
this.focus(); | |
const thisVal = this.value; | |
this.value = ""; | |
this.value = thisVal; | |
return this; | |
}; | |
/* | |
* Function that allows only Numeric input | |
* (c) 2019 James Perrin, MIT License, https://www.countrydawgg.com | |
* @param {Node} The input element key event | |
* @return {String} The Allowed keyCode | |
*/ | |
HTMLInputElement.prototype.forceNumericOnly = function () { | |
this.addEventListener("keydown", function (evt) { | |
const keyCode = | |
evt.charCode || evt.keyCode || evt.which || evt.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 = evt.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) { | |
evt.preventDefault(); | |
return false; | |
} | |
}); | |
}; | |
/** | |
* Function that Disables element property | |
* (c) 2019 James Perrin, MIT License, https://www.countrydawgg.com | |
* @param {Node} disable True (default) or False to Disable element | |
*/ | |
HTMLInputElement.prototype.disableElement = function (disable) { | |
if (typeof disable === "undefined" || disable == null) { | |
disable = true; | |
} | |
this.disabled = disable; | |
this.setAttribute("aria-disabled", disable); | |
this.setAttribute("aria-live", "polite"); | |
}; | |
/** | |
* Serialize all form data into a query string | |
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com | |
* SEE: https://gomakethings.com/how-to-serialize-form-data-with-vanilla-js/ | |
* | |
* @param {Node} this The form to serialize | |
* @return {String} The serialized form data | |
*/ | |
HTMLFormElement.prototype.serialize = function () { | |
/* | |
* This is a VanillaJS verion of $(form).serialize() | |
*/ | |
// Setup our serialized data | |
const serialized = []; | |
// Loop through each field in the form | |
for (let i = 0; i < this.elements.length; i++) { | |
const field = this.elements[i]; | |
// Don't serialize fields without a name, submits, buttons, file and reset inputs, and disabled fields | |
if ( | |
!field.name || | |
field.disabled || | |
field.type === "file" || | |
field.type === "reset" || | |
field.type === "submit" || | |
field.type === "button" | |
) { | |
continue; | |
} | |
// If a multi-select, get all selections | |
if (field.type === "select-multiple") { | |
for (let n = 0; n < field.options.length; n++) { | |
if (!field.options[n].selected) { | |
continue; | |
} | |
serialized.push( | |
encodeURIComponent(field.name) + | |
"=" + | |
encodeURIComponent(field.options[n].value) | |
); | |
} | |
} | |
// Convert field data to a query string | |
else if ( | |
(field.type !== "checkbox" && field.type !== "radio") || | |
field.checked | |
) { | |
serialized.push( | |
encodeURIComponent(field.name) + | |
"=" + | |
encodeURIComponent(field.value) | |
); | |
} | |
} | |
return serialized.join("&"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment