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
660 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

831 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