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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, dropdown 4 36
es6 typescript how to filter an array 10 38
Calculate days between two dates 7 35
jqplot Pie Chart 2 13
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 my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write 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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now