I am using the following jquery snippet to append ' UK' to a text input when the user presses enter on his/her keyboard:
jQuery('#myinput').one('keypress', function (e) {
if (e.which == 13) {
jQuery('#myinput').val($('#myinput').val() + ' UK');
return false;
}
});
However, I only want 'UK' to be appended if the text doesn't already contain 'UK'. Is there any easy way to check if 'UK' is already present using jQuery?
I have tried the following, but this appends nothing either way:
if ( !'#myinput:contains("UK")') {
jQuery('#myinput').one('keypress', function (e) {
if (e.which == 13) {
jQuery('#myinput').val($('#myinput').val() + ' UK');
return false;
}
}
});