I need to be able to go to the next line after hitting the ‘Enter’ (keycode == 13) after entering some text. This works if you hit ‘Enter’ before you enter any text but not after. Here’s some of my script
var keyPressedOnContactUsMessageHandler = function (e) {
if (e.which == 13 || e.keyCode == 13) {
$(this).append(" \n\n");
if ($("#ContactUsMessage").focus()) {
return false;
} else {
return true;
}
sendMessage();
blockEnterKeyHandler(e);
}
};
I think the problem is probably that you have a
textareaand are attempting to add content to it withappend. The HTML content of atextareaelement is only relevant when the page is parsed. After that, the text contained is referred to by thevalueproperty. You should modify this instead.My guess is that your browser is being generous: if you modify the HTML before the
valueis altered, it modifies thevalueaccordingly. It’s better just to modify thevalueyourself.Also,
$("#ContactUsMessage").focus()will always return a truthy value. If you are trying to test whether the element is focused, useis:NB that this does not work in older browsers if you are using a version of jQuery before 1.6.