I am trying to add some javascript that will allow the user of a text chat box to submit what they’ve typed by hitting the enter key instead of having to click on the send key. Got it to work in Firefox, but it doesn’t work in the two versions of IE I’ve tried.
After googling on this subject, I found the following javascript fucntion that is supposed to work with IE as well as others:
function catchEnter(e) {
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==13) {
document.messageform.submit();
document.forms[1].elements[1].value = ”;
}
}
The if(e.keyCode) line is supposed to pick up the key code, “13” of the enter key for IE browsers. The if(code==13){ } part contains the same code that I use successfully for the send button. It is supposed to submit the form and it also erased what’s on the input line.
The relevant code in the form looks like this:
<form name=”messageform” action=”handlechat.php” method=”post” target=”chatwindow” style=”margin: 0px 0px 0px 0px;”>
<iframe name=”chatwindow” width=”1″ height=”1″ align=”center” scrolling=”no”>
</iframe>
<br />
<b>Character Name:</b> <input type=”text” name=”array[name]” size=”38″ maxlength=”50″ />
<br />
<div>
<b>Message:</b> <input type=”text” name=”array[message]” id=”messg” size=”65″ maxlength=”500″ onKeyPress=”catchEnter(event)” />
</div>
print <input type=”button” name=”send” value=”Snd” style=”width: 35px” onClick=”sendText()”/>
</form>
Notice that there are two input lines in the form.
(Also note: the sendText function is another function that takes care of the send button press:
function sendText() {
document.messageform.submit();
document.forms[1].elements[1].value = ”;
}
– it works fine and is not an issue here.)
This all works fine when I use the enter key or the send button in Firefox. In IE 5 and 6, nothing gets submitted when I press the enter key, but the text disappears like it’s supposed to. The send button works OK.
Does anyone know what might be going wrong in IE?