Skip to content

Instantly share code, notes, and snippets.

@jamilservicos
Last active October 6, 2020 15:53
Show Gist options
  • Save jamilservicos/1d2e2dceb1529fe2e27b5fb18f30d037 to your computer and use it in GitHub Desktop.
Save jamilservicos/1d2e2dceb1529fe2e27b5fb18f30d037 to your computer and use it in GitHub Desktop.
simple example of uppercase, lowercase and capitalize text
/*
simple example to capitalize
*/
function capitaLize() {
const textbox = document.getElementById("text-box"); //textarea
const textresult = document.getElementById("text-result"); //result element "<pre>";
if ((textbox) && (textbox.value) && (textbox.value.toString().trim().length > 0)) {
textresult.innerText = "";
const str = textbox.value.toString().trim().toLowerCase(); //turns everything to lowercase
const length = str.length;
if (length > 0) {
textresult.innerText = str.toString().replace(/\S+/g, words => { //separate by word blocks
return words.charAt(0).toUpperCase() + words.substr(1); //to uppercase first character
});
return true;
}
}
//
}
/*
simple example to lowercase
*/
function lowerCase() {
const textbox = document.getElementById("text-box"); //textarea
const textresult = document.getElementById("text-result"); //result element "<pre>";
if ((textbox) && (textbox.value) && (textbox.value.toString().trim().length > 0)) {
textresult.innerText = "";
const str = textbox.value.toString().trim();
const length = str.length;
if (length > 0) {
textresult.innerText = str.toString().toLowerCase();
return true;
}
}
//
}
/*
simple example to uppercase
*/
function upperCase() {
const textbox = document.getElementById("text-box"); //textarea
const textresult = document.getElementById("text-result"); //result element "<pre>";
if ((textbox) && (textbox.value) && (textbox.value.toString().trim().length > 0)) {
textresult.innerText = "";
const str = textbox.value.toString().trim();
const length = str.length;
//
if (length > 0) {
textresult.innerText = str.toString().toUpperCase();
return true;
}
}
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment