Link to home
Start Free TrialLog in
Avatar of mmahon512
mmahon512

asked on

Help with Javascript to replace enter key code with the tab key code that works in both IE 7, 8 & Firefox

I have tried several solutions but none seems to correctly work in all browsers.  This code works fine in IE but it fails to tab to the next form element in line instead it forces the page to submit.

    <script language="javascript" type="text/javascript">
        function enterToTab(evt) {
            var pressedKey;
            if (!evt) var evt = window.event; {
                pressedKey = evt.which || evt.keyCode;
            }
            alert("Pressed Key: " + formRef);
            for (var i = 0, e = formRef.elements, len = e.length, hasNext = true; i < len && hasNext; i++)
                if (e[i].type && /^text|password|file/.test(e[i].type)) {
                for (var j = i + 1; j < len && (!e[j].type || /submit|reset/.test(e[j].type) || (focusAny ? /hidden/.test(e[j].type) : !/^text|password|file/.test(e[j].type))); j++);
                hasNext = j != len;
                e[i].onkeypress = (function(index, notLast) {
                    return function() {
                        var ta = false, k = (arguments[0] ? arguments[0].which : pressedKey) != 13;
                        if (!k && !(ta = (this.type == 'textarea' && this.value.length > 0)) && notLast)
                            this.form.elements[index].focus();
                        return k || ta;
                    }
                })(j, hasNext);
            }
        }    
    </script>
...
<body onload="ResizeContentAreaOverall();" onresize="ResizeContentAreaOverall();" onkeydown="enterToTab(event);">

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

@mmahon512: It would be much easier to debug, if you could tell at which exact line of javascript the error occurs.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmahon512
mmahon512

ASKER

The link provided answered the question and helped me solve the problem.  I had to tweak it a little to fit but it gave me a great starting point.
Thanks for the points!