I am simply listening for a keyup event of an input element and gather the results into a string like so
word=word+String.fromCharCode(key.keyCode);
The problem is that the word is in capital letters while I want it to be case-sensitive. For example if I type abcef my accumulated word becomes 'ABCEF' .
Note - I need a pure javascript solution (no libraries..) Any thoughts?
The most basic way to do case insensitive string comparison in JavaScript is using either the toLowerCase() or toUpperCase() method to make sure both strings are either all lowercase or all uppercase.
To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.
The String. fromCharCode() method converts Unicode values to characters. The String. fromCharCode() is a static method of the String object.
Events like keyup
and keydown
will return 65 for both a
and A
(and also true
for event.shiftKey
if that key is held down).
The keypress
event returns different keycodes for upper and lower case letters, so to get this working case sensitive you should use the keypress
event, and fromCharCode()
will return the correct letter, case sensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With