astralaaron Posted September 5, 2008 Share Posted September 5, 2008 I made a little instant messenger with php on my website, you type into a textarea, I was wondering if it is possible while the text curser is inside of the textarea to be able to submit the form when pressing enter.please help :] Link to comment Share on other sites More sharing options...
astralaaron Posted September 5, 2008 Author Share Posted September 5, 2008 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 More sharing options...
astralaaron Posted September 5, 2008 Author Share Posted September 5, 2008 hey guys, i found the answer to this also, turned out to be pretty simple! :<body onload="document.chat_form.msg.focus()"><body onload="document.FORMNAME.INPUTNAME.focus()"> Link to comment Share on other sites More sharing options...
Kingy Posted September 5, 2008 Share Posted September 5, 2008 Right I clicked the reply button and saw your message come up to say you found an answer. Yes that's the easiest way to do it. Link to comment Share on other sites More sharing options...
jeffman Posted September 5, 2008 Share Posted September 5, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.