- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Chat2
I followed the tutorial on Chat2 and I can’t send the message. I press enter and it just adds another line to the text box. What is going on and what can I do to fix it?
Unless we can see your code, we can’t help
Do you have a live code example of what you’ve tried to implement?
Without seeing the code we can only guess, but I think I know what your problem is.
As far as I can tell <input />
tags by default submits the form when you press the enter key while <textarea />
does not.
If you don’t need multiple lines you can just change the <textarea />
to <input />
and everything should be fine
You can force <textarea />
to submit the form with Javascript:
<form>
<textarea id="multiline"></textarea>
</form>
$('#multiline').on('keypress', function(e) {
if(e.which == 13) {
e.preventDefault();
$('form').submit();
}
});
And if you want to have multiline support with let’s say CTRL+Enter, just add this to the keypress callback function:
if(e.ctrlKey && e.which == 10) { //when ctrl is pressed enter input code is 10 instead of 13
$(this).append("\n"); // \n = line break
}