[thelist] Stop posting from fields (was: Can't submit form data with 'enter' key in IE)

Bill Moseley moseley at hank.org
Sat Mar 11 01:53:56 CST 2006

On Tue, Mar 07, 2006 at 11:23:16AM -0800, Jim Davis wrote:
> I have a form with a text field for input and a submit button used to search
> a database. In Fire Fox, entering text in the text field and hitting the
> keyboard 'enter' key will return the search result.

My question is somewhat the opposite.  How can I *prevent* firefox
from submitting forms when hitting the 'enter' key?

I currently have an onsubmit confirm dialog, but it would be nicer to
just prevent it.

I see I also have code in an attempt to stop it -- can't remember
where I got this, but it's not working.  (This is assigned using
Behaviour, btw.)

I see I attempted to fix this at one point:

    /* Try and avoid submit on text field */
    '.inputText' : function(e){
        e.onkeydown = function(event) {
            var ie4 = (document.all)? true:false;
            var safari = navigator.userAgent.indexOf('Safari')>0;
            var evt = (event) ? event : ((window.event)?window.event:new Object);
            var keyCode = document.layers ? evt.which : document.all ?  evt.keyCode : evt.keyCode;

            if (safari && keyCode==3) keyCode = 13;

            // IE5 on Mac sends an enter as KeyCode 0, instead of 13.  But, a lot of other codes show up as 0.
            if (ie4 && keyCode==0) keyCode = 13;

            if (keyCode == 13 ) {
            // We need to stop this enter key.
                evt.cancelBubble = true;
                if (evt.preventDefault) evt.preventDefault();
                if (evt.returnValue) evt.returnValue = false;
                if (evt.stopPropagation) evt.stopPropagation();
                return false;
    } // 'text'

Bill Moseley
moseley at hank.org

More information about the thelist mailing list