Reputation: 4663
I want to convert letters automatically on keypress, tried this
$(document).on('keypress', function(e) {
$("#nick").val().toLowerCase();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id=nick value="">
But when I write Uppercase letters, it doesnt convert to lowercase. Where I did mistake ?
Upvotes: 2
Views: 3512
Reputation: 1433
you can also use below approach.
$(document).on('keyup','#nick',function(e){
$(this).val(e.currentTarget.value.toLowerCase());
})
$(document).on('keyup','#nick',function(e){
$(this).val(e.currentTarget.value.toLowerCase());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="nick" value="">
Upvotes: 0
Reputation: 7207
you gotta use the converted text somewhere, right? :)
$(document).on('keypress', function(e) {
$("#nick").val($("#nick").val().toLowerCase());
});
UPDATE
if you use keyup
it'll work as desired: DEMO
$(document).on('keyup', function(e) {
$("#nick").val($("#nick").val().toLowerCase());
});
Upvotes: 1
Reputation: 241188
You aren't modifying the existing value. You need to re-assign the lowercase value:
$(document).on('keypress', function(e) {
var value = $("#nick").val().toLowerCase();
$("#nick").val(value);
});
Since the keypress
event won't change the last character, I would suggest listening to the input
event instead. The keyup
event would work as well.
$(document).on('input', function (e) {
e.target.value = e.target.value.toLowerCase();
});
Without jQuery:
document.addEventListener('input', function (e) {
e.target.value = e.target.value.toLowerCase();
});
Upvotes: 4