cpd Posted July 22, 2009 Share Posted July 22, 2009 In a form, how do i make the page NOT refresh when hitting the "Enter" key after typing in a text box <form method="post"> <input type="text" name="message"> </form> say i was focused on that text box how do i make it so that when the form is submitted it doesnt refresh the page? Cheers Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 22, 2009 Share Posted July 22, 2009 In a form, how do i make the page NOT refresh when hitting the "Enter" key after typing in a text box <form method="post"> <input type="text" name="message"> </form> say i was focused on that text box how do i make it so that when the form is submitted it doesnt refresh the page? Cheers this isn't possible, because submitting the form sends a new request to the server and therefore forces a refresh. what exactly are you trying to achieve? Quote Link to comment Share on other sites More sharing options...
dzelenika Posted July 22, 2009 Share Posted July 22, 2009 You should use onkeypress event on your form: onkeypress="isEnter(this, event) then handle event to check is enter pressed: function isEnter(obj, evt) { var key= new Number; if (window.event) { key = window.event.keyCode; } else if(evt) { key = evt.which; } else { return true; } if (key == 13) { return true; } } Quote Link to comment Share on other sites More sharing options...
cpd Posted July 22, 2009 Author Share Posted July 22, 2009 Just for everybodies benefit ive found a way of doing it thats really easy and simple. function searchKeyPress(e){ if(window.event){ e = window.event; } if(e.keyCode == 13){ // Code to execute } } then in your input tag that IS NOT wrapped in <form> tags onkeypress="searchKeyPress(event);" Quote Link to comment Share on other sites More sharing options...
cpd Posted July 23, 2009 Author Share Posted July 23, 2009 after using this function that i found a few times, some people have said that when they type the keys dont register. does anyone know why? Quote Link to comment Share on other sites More sharing options...
dzelenika Posted July 23, 2009 Share Posted July 23, 2009 after using this function that i found a few times, some people have said that when they type the keys dont register. does anyone know why? On which browser? Quote Link to comment Share on other sites More sharing options...
cpd Posted July 23, 2009 Author Share Posted July 23, 2009 Both people who have tried my chat feature were using Firefox and i am using firefox too however it works fine for me. It was on a local server and ive uploaded it to an online server and it works ok, could that of been the problem? Quote Link to comment Share on other sites More sharing options...
dzelenika Posted July 23, 2009 Share Posted July 23, 2009 Both people who have tried my chat feature were using Firefox and i am using firefox too however it works fine for me. It was on a local server and ive uploaded it to an online server and it works ok, could that of been the problem? Javascript doesn't depends on server. The way you are checking pressed key is preferred in IE. I think, but not 100% sure, that onKeyPress should return value to indicate that handled event. Anyway look at: http://www.w3schools.com/jsref/jsref_onkeypress.asp Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted July 23, 2009 Share Posted July 23, 2009 Or submit it to an iframe. The iframe can be hidden with CSS <iframe name="frame"></iframe> <form method="post" target="frame"> <input type="text" name="message"> </form> Or, if you don't want the form to submit at all: <form method="post" onsubmit="return false;"> <input type="text" name="message"> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.