Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
682 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

664 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