Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simulate tab key with enter key

Status
Not open for further replies.

rotsey

Programmer
Nov 23, 2004
135
0
0
AU
Hi,

I wan to use OnkeyPress on a textbox so that when the enter key is pressed it replaces with the tab key.

In other words moves the focus to next control when enter key is pressed

any help appreciated
rotsey

 

Do you want to move to the next control in source order, or the next control in tabIndex order?

Also, when you say "textbox", do you mean an input element with a type attribute of "text", or do you mean a textarea element?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
hi
next control in tabindex order.

yes i mean input type=text.


sorry i am using ASP.NET
 
here's something i put together a while ago that you might find useful:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--

function tabNext(e, i) {
    if (e.keyCode == 13)
	    document.forms['f'].elements[i].focus();
	return false;
}

-->
</script>

<style type="text/css">

</style>

</head>

<body>
<form name="f">

  <input type="text" name="t1" onkeypress="return tabNext(event, 't2');" /><br />
  <input type="text" name="t2" onkeypress="return tabNext(event, 't3');" /><br />
  <input type="text" name="t3" onkeypress="return tabNext(event, 't4');" /><br />
  <input type="text" name="t4" onkeypress="return tabNext(event, 's');" /><br />
  <input type="submit" name="s" />
</form>
</body>

</html>

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
i wrote this a while back... it's a bit lengthier than clflava's example, but it will automatically attach itself to all fields (except hidden, submit, button and textarea) on all forms on your page

Code:
/**
 * makes the enter key work as tab key on forms
 */
function doEnterToTab(e) {
	var evt = e ? e : event;	
	
	if (evt.keyCode == 13) {
		try {
			this.form.elements[this.form.tabOrder[(this.index + 1)]].focus();
		} catch(E) {}
		return false;
	}
}

/**
 * attaches doEnterToTab() to form elements,
 * except SUBMIT, BUTTON and TEXTAREA
 */
function attachChangeEnter() {
	for (var y = 0; y < document.forms.length; y++) {
		var f = document.forms[y];
		f.tabOrder = [];
		var els = f.elements;
		for (var x = 0, ndx = 0; x < els.length; x++) {
			if (!els[x].type
					|| els[x].type.toLowerCase() == "hidden") {
				continue;
			}
			else {
				f.tabOrder[f.tabOrder.length] = x;
				els[x].index = ndx++;
				if (els[x].type.toLowerCase() != "textarea"
					&& els[x].type.toLowerCase() != "submit"
					&& els[x].type.toLowerCase() != "button")
					els[x].onkeyup = doEnterToTab;
			}// end else
		}// end for x
	}// end for y
}// end attachChangeEnter()

if (!window.addEvent) {
	// 'borrowed' from mishoo's Calendar
	function addEvent(el, evname, func) {
		if (el.attachEvent) { // IE
			el.attachEvent("on" + evname, func);
		} else if (el.addEventListener) { // Gecko / W3C
			el.addEventListener(evname, func, true);
		} else {
			el["on" + evname] = func;
		}
	}
}

//  attach to all forms
addEvent(window, "load", attachChangeEnter);

just save it as a .js file and include it in your page - it does the rest

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
thanks guys i came up with clFlava's way and did it like that.

So you can't trap the enter key and replace it with a tab key in the onkeypress event, Strange?
 
LOL. Its not that dramatic.

All I i'm saying is like this


if (e.KeyCode==13)
e.KeyCode=9;
 

It might not be as dramatic, but think about the message they are giving... If it were possible to do what you are asking, what they were saying would also be possible - which would be quite a big security risk.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top