Solved

Using enter instead of tab (but with a button involved)

Posted on 2014-10-23
8
84 Views
Last Modified: 2014-10-23
I have the following code to allow the use of the enter key instead of tab to move through the fields on my form

<script language="javascript">
document.onkeydown = KeyPress;
function KeyPress() {
if (event.keyCode == 13 && event.srcElement.type != 'textarea')
{
event.keyCode=9;
}
}
</script>

I need to also allow the enter key to be pressed when a button is involved, <input type=button ...>, but with the above code the enter is replaced with a tab.  What do I need to change.
0
Comment
Question by:jdthedj
  • 5
  • 3
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
document.onkeydown = KeyPress;
function KeyPress(event) {
    elem = (event.srcElement ? event.srcElement : event.target);
    if (event.keyCode == 13 && elem.type != "button" && elem.type != 'textarea') {
        event.target.nextSibling.nextSibling.focus();
        event.preventDefault
    }
}

Open in new window

0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Thanks for the reply Gary.  I cannot get the code to do anything.  I put in an alert at the start of the function and it fires, so it is getting to the code OK, but nothing happens.  I am using IE 11
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Code works fine
http://jsfiddle.net/9qxpvt0a/2/

Do you have a working link, this will depend on your markup
0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
I am working in our intranet so it is not outside.  Here is the whole code of the test page.

<FORM NAME="Test2" ACTION="Test2.htm" METHOD="Post">

<SCRIPT type="text/JavaScript">
<!-- hide
function ProcessForm(Nme)
{
if (Nme=="Get")
{
document.Test2.action="Test2.htm";
document.Test2.submit();
}
}
// stop hiding -->
</script>

<script language="javascript">
document.onkeydown = KeyPress;
function KeyPress(event) {
    elem = (event.srcElement ? event.srcElement : event.target);
    if (event.keyCode == 13 && elem.type != "button" && elem.type != 'textarea') {
        event.target.nextSibling.nextSibling.focus();
        event.preventDefault
    }
}
</script>

<DIV ID="Control1" STYLE="position:absolute; top: 40px; left: 100px; width: 100px; height: 22px;">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
<TR VALIGN=top><TD><DIV align="Left">

<input type=text name="One" size=10><br>
<input type=text name="Two" size=10><br>
<input type=button value="Go" onClick="ProcessForm('Get')">

</DIV></TD></TR></TABLE></DIV>
</FORM>

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Thanks Gary - After more experimentation I have got it working with the following code

<script language="javascript">
document.onkeydown = KeyPress;
function KeyPress() {
var elem = (event.srcElement ? event.srcElement : event.target);
if (event.keyCode == 13 && event.srcElement.type != 'textarea' && elem.type != 'button')
{
event.keyCode=9;
}
}
</script>
0
 
LVL 3

Author Closing Comment

by:jdthedj
Comment Utility
The line that provided the solution was

elem = (event.srcElement ? event.srcElement : event.target);
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Use this instead, doesn't rely on consecutive elements

http://jsfiddle.net/9qxpvt0a/6/

document.onkeydown = KeyPress;

function KeyPress(event) {
    elem = (event.srcElement ? event.srcElement : event.target);
    if (event.keyCode == 13 && elem.type != "button" && elem.type != 'textarea') {
        el = elem.nextSibling;
        while (el.type != "text" && el.type != "textarea" && el.type != "button") {
            el = el.nextSibling;
        }
        el.focus();
        event.preventDefault
    }
}

Open in new window

0
 
LVL 3

Author Comment

by:jdthedj
Comment Utility
Great! - thanks Gary
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now