Solved

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

Posted on 2010-09-03
4
662 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:mmahon512
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33600457
@mmahon512: It would be much easier to debug, if you could tell at which exact line of javascript the error occurs.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33600463
0
 

Author Closing Comment

by:mmahon512
ID: 33621142
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.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33622487
Thanks for the points!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question