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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble making keypress work 1

Status
Not open for further replies.

romh

Programmer
Jan 3, 2003
297
US
I have tried everything. I'm not a javascript expert by any means, but i was wondering if you guys could look at my code and tell me why the keypress isnt working. It works fine on Netscape and Firefox, but not on Internet Explorer. I am trying to fire the keypress event on an asp.net textbox in order for it to move to the textbox once the maximum number of characters have been enterered into the text box. Thanks alot.

<html>
<body>
<head>
<title>CFS</title>


<script LANGUAGE = "javascript">

function tabber4()
{
var num =SSNa.value;
if((num.length)==SSNa.maxLength-1)
SSNb.focus();
}
</script>

</head>


<form runat="server">
<asp:Textbox id="SSNa" runat="server" maxlength="3" tabindex="7" onkeypress="return tabber4();" style="width:30"/> -
<asp:Textbox id="SSNb" runat="server" maxlength="2" tabindex="8" style="width:20"/> -

</form>
</body>
</html>
 
I believe each time you refer to the element, you would have to include a reference to the document and form e.g.
Code:
document.Form1.SSNa.value


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Or, use the "getElementById()" method, to make sure that you are getting the precise element:

Code:
<script LANGUAGE = "javascript">

function tabber4()
{
 var num = document.getElementById("SSNa").value;
 if((num.length)==document.getElementById("SSNa").maxLength-1)
   document.getElementById("SSNb").focus();
}
</script>

Better yet, pass the relevant objects into your script:

Code:
<script LANGUAGE = "javascript">

function tabber4(currObj,nextObj)
{
 var num =currObj.value;
 if((num.length)==currObj.maxLength-1)
   nextObj.focus();
}
</script>

And the mark-up:

Code:
<asp:Textbox id="SSNa" runat="server" maxlength="3" tabindex="7" onkeypress="return tabber4(this,document.getElementById('SSNb');" style="width:30"/> -
<asp:Textbox id="SSNb" runat="server" maxlength="2" tabindex="8" style="width:20"/> -

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
okay, thanks. I havent tried any of the above solutions yet. I did figure out that if i include everything in 1 statement, it does work. Example:

<asp:Textbox id="SSNa" runat="server" maxlength="3" tabindex="7" onKeyPress="if((SSNa.value.length)==SSNa.maxLength) SSNb.focus();" style="width:30"/>


I dont know if this solution is more elegant. It probably has something to do with getting the precise element.


Thanks alot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top