How do I move the cursor from the first textarea to a second textarea, without using tab or the mouse?
I'm building a password box that holds three text areas. Each text area has one character. After I type the first character of the password, I have to press tab or use the mouse to get to the second tex开发者_StackOverflow社区t area to type the second character of the password. I would like to make this happen automatically (cursor movement) just right after I type in the first text area.
how can I achieve this ?
If you may ask, I'm using Visual Studio .NET 2008 in C# I'm a perfect newbie in .net and I don't know how to ask this question with the appropiate words.
Thank you.
Try onKeyPress. That should take care of what you are looking for.
<input type="text" name="password" onKeyPress="autoTab()" />
<script type="text/javascript" language="JavaScript">
function autoTab() {
//do stuff
}
</script>
Here's a tutorial that deals with changing the cursor position of a field.
http://www.webdeveloper.com/forum/showthread.php?t=91817
This suggests your autoTab() function should look like this.
function autoTab(field,nextFieldID){
if(field.value.length >= field.maxLength){
document.getElementById(nextFieldID).focus();
}
}
Are you using jQuery in your project?
<!DOCTYPE html>
<html>
<head>
<title>example</title>
<style type="text/css">
*
{
margin: 0;
padding: 0;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#data1').keyup(function () {
if ($(this).val().length == 1) {
$('#data2').focus();
}
});
});
</script>
</head>
<body>
<input id="data1" type="text" value="" style="width: 10px" /><br />
<input id="data2" type="text" value="" style="width: 10px" />
</body>
</html>
if textbox1.text.length > 0 then textbox2.focus();
精彩评论