Jump to content

Submit a form with 'enter' key


astralaaron

Recommended Posts

I found this code that works:

<script type="text/javascript"><!--function submitenter(myfield,e){var keycode;if (window.event) keycode = window.event.keyCode;else if (e) keycode = e.which;else return true;if (keycode == 13){document.chat_form.submit();return false;}elsereturn true;}//--></script><form name="chat_form"><textarea onKeyPress="return submitenter(this,event)" ></textarea></form>

and I have a new question.. is it possible when a window loads to have the mouse curser already inside of that textarea ready to type again?

Link to comment
Share on other sites

Any <input type="text"> will submit a form when enter is pressed. If you really mean a <textarea>, you might reconsider. Users expext the return key to enter a line break. Messing with that can upset people. You should generally stick with the familiar user interface if you want happy users.As to focusing a text element onload, I always recommend using a dedicated onload handler. It makes updating your code a whole lot easier:

function init () {   document.getElementById("myText").focus();   // Future statements go here;	// Future statements go here;	// Future statements go here;	// Future statements go here; }window.onload = init;

Eventually there comes a time when you want your onload handler to do multiple things. Starting off with this framework lets you add a new statement very easily, without pulling your code apart or wondering how it's done. I have webapps with literally dozens of statements in the onload handler, mostly assigning functions to buttons and menu items.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...