Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

javascript: add enter key = tab after initial load of form

Using jquery and/or javascript, how can I make the enter key same as tab, but have that happen after a button is clicked on the form, in other words not onload of the form?

The following which I found via good search works if embedded in the body tag, but that does not work for me since I need the feature to occur post-load as mentioned above.

"if(event.keyCode==13) {event.keyCode=9; return event.keyCode })"
0
KeithMcElroy
Asked:
KeithMcElroy
  • 8
  • 5
1 Solution
 
GaryCommented:
You can't change an enter to become a tab (not like that anyway)
Are you wanting to capture the enter key so you can set focus on the next input?
0
 
KeithMcElroyAuthor Commented:
I tried this at the event where I want the 'enter to tab' feature to work

                  var body = $('body')

                  body.addEventListener("onkeydown", "if(event.keyCode==13) {event.keyCode=9; return event.keyCode }", false)


It does not work, however, maybe it is close?
0
 
KeithMcElroyAuthor Commented:
Yes, on set focus on next input
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GaryCommented:
Are you using jQuery or just plain javascript?
0
 
KeithMcElroyAuthor Commented:
jquery, tried this (no success) but seems close

                  $("input").live('keyup',function () {
                  $(this).next(".input").focus();
                        });
0
 
KeithMcElroyAuthor Commented:
corrected the class . still no work

  $("input").live('keyup',function () {
                  $(this).next("input").focus();
                        });
0
 
GaryCommented:
$("input").keypress(function(e) {
	if(e.which == 13) {
		var index = $('input[type=text]').index(this) + 1;
		var total_els = $('body').find('input[type=text]').length;
		if(total_els == index){index=0}
		$('input[type=text]:eq('+index+')').focus();
	}
});

Open in new window

0
 
KeithMcElroyAuthor Commented:
Thank you for the code, it is not working yet, but may be something about my form. I understand the steps so I bet I shall be able to make it work.
Will stand by for any further comments, otherwise shall reward points.
0
 
GaryCommented:
Post your code, it may need tweaking a tiny bit
0
 
KeithMcElroyAuthor Commented:
I think I see where I need help, this is an internal web form, dynamic and I've never been able to figure out how to get a working sample outside the internal network,
However, the code you did, as is, is working except in rows where I have disabled text boxes and check boxes.  So, I think, if I can remove disabled inputs and check boxes from the forward cursor, it is good.  I will pursue, but if you know off top of your head, how to do that would be be great.  This is a big help.  Thank you!  Again will reward points, but want to allow for any further from you.
0
 
KeithMcElroyAuthor Commented:
the following did the trick:
                  $("input").keypress(function(e) {
                  if(e.which == 13) {
                  
                        var index = $('input[type=text]:enabled').index(this) + 1;
                        var total_els = $('body').find('input[type=text]:enabled').length;
                              if(total_els == index){index=0}
                                    $('input[type=text]:enabled:eq('+index+')').focus();
                        }
                  });
0
 
KeithMcElroyAuthor Commented:
Awesome, thank you!
0
 
GaryCommented:
So you want to skip disabled elements?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now