Convert Text Cases using jQuery, without CSS

0 Shares
0
0
0

Let us fetch the string to be converted and have it in a variable, so we reuse it for different cases.
str = $(‘selector’).text(); // Let’s assume str = ‘mac-Book pRO’
1. To Lowercase
$(‘selector’).text(str.toLowerCase()); // str = ‘mac-book pro’
2. To Uppercase
$(‘selector’).text(str.toUpperCase()); // str = ‘MAC-BOOK PRO’
3. To Sentence case
Capitalise the first letter of the first word in the sentence or heading for sentence case.
$(‘selector’).text(str.charAt(0).toUpperCase() + str.substr(1).toLowerCase()) // str = ‘Mac-book pro’
Here, str.charAt(0).toUpperCase() converts the first character of the string to uppercase.
str.substr(1) – fetches the substring from the string starting from index 1 till the end.
str.substr(1).toLowerCase() – converts the extracted sub-string to lowercase
Reference: https://www.smartherd.com/convert-text-cases-using-jquery-without-css/