i have a form with a textfield and a submit button. for some reason, i do want the submit button responses to the 'Enter' key when the user press 'Enter' when the focus is in the textfield. Is there anyway to do so?
thanks a lot.
i have a form with a textfield and a submit button. for some reason, i do want the submit button responses to the 'Enter' key when the user press 'Enter' when the focus is in the textfield. Is there anyway to do so?
thanks a lot.
You would want to use Javascript to accomplish this. I don't have a code snippet at the moment, but you should be able to find one easily by doing a search through the forums.
Hope this helps.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
you can use javascript
usingPHP Code:<script language="javascript">
function send()
{
document.getElementById('from1').submit();
}
</script>
HTML Code:<button name="button" id="button" value="Submit" onClick="javascript: send()" />Login
noorahmad, I think the OP was intending the opposite of what you accomplished. See the thread title.
This script cancels the submission if it was triggered by the enter key. An example is provided below. (I don't really like the name, but it's the best I could come up with.)
EDIT: Woops, it actually doesn't work. I could have sworn it did.Code:function noEnterSubmit(form){ if(typeof form == 'string'){ form = document.forms.namedItem(form); if(form == null) return false; } var enter = false; function keyEvent(event){ event = event || window.event; enter = (event.keyCode || event.charCode) == 13; } function submit(){ if(enter){ enter = false; return false; } } var events = ['keypress', 'keydown']; for(var i = 0; i< events.length; i++){ if(form.addEventListener) form.addEventListener(events[i], keyEvent, true); else if(form.attachEvent) form.attachEvent('on' + events[i], keyEvent); } if(form.addEventListener) form.addEventListener('submit', submit, true); else if(form.attachEvent) form.attachEvent('onsubmit', submit); }Code:<html> <head> <title>No-Enter Submission</title> <script type="text/javascript" src="NoEnterSubmission.js"></script> <script type="text/javascript"> window.onload = function (){ noEnterSubmit('test'); }; </script> </head> <body> <form method="get" id="test"> <input type="text"> <input type="submit"> </form> </body> </html>
Last edited by Jesdisciple; 06-21-2009 at 12:33 AM.
I found why it didn't work. I had initially used the old event registration model, and then moved to the new one. At first, I didn't know how to cancel the event using the new model, but I finally found it. So here's a standard solution which is backwards-compatible with the old model.Code:function noEnterSubmit(form){ if(typeof form == 'string') form = document.forms.namedItem(form); if(typeof form != 'object' || form == null || form.constructor != HTMLFormElement) return false; var enter = false; function keyListener(event){ event = event || window.event; enter = (event.keyCode || event.charCode) == 13; } function submit(event){ if(enter){ event = event || window.event; event.preventDefault(); enter = false; return false; } } form.registerEventListener('keypress', keyListener, true); form.registerEventListener('keydown', keyListener, true); form.registerEventListener('submit', submit, true); } HTMLElement.prototype.registerEventListener = function(type, listener, useCapture){ if(this.addEventListener) this.addEventListener(type, listener, useCapture); else if(this.attachEvent) this.attachEvent('on' + type, listener); else this['on' + type] = listener; };
Last edited by Jesdisciple; 06-21-2009 at 12:54 AM.
Just a note for anyone who read my last post before I updated it. I found the standard way to cancel the event, so use the current code.
Thq Chris,the above code is working fine,but the is a javascript error 'HTMLFormElement' is undefined.
Hi Vijaya. Which browser and version are you using? Also, please give the output of the following function in the same page, with the same argument as the other function:Code:function test(form){ if(typeof form == 'string') form = document.forms.namedItem(form); alert(form.constructor); }
Last edited by Jesdisciple; 09-17-2009 at 01:31 PM.
Vijaya (09-24-2009)
Hi chris,
I am using IE6 and i am working with master pages,In master page i am calling the function as
window.onload = function (){noEnterSubmit('aspnetForm');};
An easy way to stop this is to convert the use of the Enter button in to a Tab for each input field.
All you need to do is to add this code to each input field:
An example :HTML Code:onKeyDown="if(event.keyCode==13) event.keyCode=9;"
HTML Code:<input type="text" name="name" size="35" id="name" onKeyDown="if(event.keyCode==13) event.keyCode=9;" />
Bookmarks