I need to make it so if a user types in any combination of the word “Quebec” it automatically rename it to Quebec City, this needs to happen in any event. The problem I’m having is if there’s any whitespace in the word it just won’t work. I tried trimming the string but it didn’t function correctly. Here’s my code, let me know if there’s anything I can improve on too.
$('#location').on('blur', function() {
var field = $('#location').val();
$.trim(field);
switch (field) {
case 'Quebec' :
case 'QUEBEC' :
case 'Québec' :
$('#location').attr('value', 'Quebec City');
break;
}
})
$('#location').on('keypress', function() {
var field = $('#location').val();
$.trim(field);
switch (field) {
case 'Quebec' :
case 'QUEBEC' :
case 'Québec' :
$('#location').attr('value', 'Quebec City');
break;
}
})
You should return value back to the
fieldvariable:Additionally, I suggest you to use
keyupevent instead ofkeypress.