Solved

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

Posted on 2014-10-23
8
86 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
ID: 40400509
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
ID: 40400538
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
ID: 40400541
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
ID: 40400551
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:jdthedj
ID: 40400570
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
ID: 40400574
The line that provided the solution was

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

Expert Comment

by:Gary
ID: 40400575
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
ID: 40400593
Great! - thanks Gary
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

911 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

26 Experts available now in Live!

Get 1:1 Help Now