Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Suppress submit button from responding to 'Enter' key

  1. #1
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Suppress submit button from responding to 'Enter' key

    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.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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

  3. #3
    Join Date
    Jun 2009
    Location
    Kabul Afghanistan
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    you can use javascript
    PHP Code:
    <script language="javascript">
    function 
    send()
    {
        
    document.getElementById('from1').submit();
    }
    </script> 
    using
    HTML Code:
    <button name="button" id="button" value="Submit" onClick="javascript: send()" />Login

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thq Chris,the above code is working fine,but the is a javascript error 'HTMLFormElement' is undefined.

  8. #8
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  9. The Following User Says Thank You to Jesdisciple For This Useful Post:

    Vijaya (09-24-2009)

  10. #9
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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');};

  11. #10
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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:

    HTML Code:
    onKeyDown="if(event.keyCode==13) event.keyCode=9;"
    An example :

    HTML Code:
    <input type="text" name="name" size="35" id="name" onKeyDown="if(event.keyCode==13) event.keyCode=9;" />

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •