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

on tab out of text box

Status
Not open for further replies.

blueindian1

Programmer
Apr 24, 2001
150
US
Hello again..

anyone know how to fire a javascript when a users tabs out of a text box?

there seems to be no event handler for this.

thanks,

rich
 
In IE, you can use this function:

Code:
function CatchTabs()
{
    if (event.keyCode==8) //8 is for tab
    {
        //Do stuff
    }
    return true; //return false to disable the key
}

And in the textbox, put:

Code:
... onKeyPress="return CatchTabs();" ...

This does not work in Netscape, so don't even try it in that poor excuse for a web browser. ;-)

HTH
 
tried that, but the tab key won't call the script. if i press any alphanumeric key, the script gets called. but if i press tab, or ctrl, or capslock or anything else the script does no fire.

any idea?
 
hmm... I haven't done this for a while, maybe I got the event wrong. Try onKeyDown instead?
 
onblur is as close as your gonna get then:

<input type=text onblur=&quot;alert('lost focus')&quot;>
 
it is keydown rather than keypress. kinda weird. and the keycode is 9 rather than 8
 
Right... I meant 9. :-D 8 is backspace :)

keyDown fires for all keys; keyPress only fires for an alphanumeric key.

This code will only work for tabbing out of the text box; not if the user clicks somewhere else. For that, you should use sjravee's suggestion of onblur.
 
yes, i only want it to work on tab out, so that i can set the focus to another text box. i wanted to control the tab order, but only for 10 text boxes out of 172 on the form.

thanks! :)
 
yes, but as far as i know if you do that you have to give all the fields tab indexes. imagine a form with 20 elements. if i set the tab index of element 15 to 2, whenever i hit tab the first time it will jump to that field, when acutally in don't want to go there yet.

i other words, i needed to control the tab index on just elements 15 through 20.

 
good point, does this help:


<html>
<head></head>
<body>
<html>
<head>
<script>
function checkInput(val,eleIndex)
{
if (val==9) {
//alert(eleIndex+1)
if (eleIndex+1>0 && eleIndex+1<6)
{
alert(document.forms[0].elements[eleIndex+1].name)
document.forms[0].elements[eleIndex+1].focus()
return false
}
}
}
</script>
</head>
<body>
<form action=&quot;somepage.asp&quot; method=get>
1:<input type=text name=&quot;text1&quot; onkeydown=&quot;return checkInput(event.keyCode,1)&quot;><br>
2:<input type=text name=&quot;text2&quot; onkeydown=&quot;return checkInput(event.keyCode,2)&quot;><br>
5:<input type=text name=&quot;text3&quot; onkeydown=&quot;return checkInput(event.keyCode,3)&quot;><br>
4:<input type=text name=&quot;text4&quot; onkeydown=&quot;return checkInput(event.keyCode,4)&quot;><br>
3:<input type=text name=&quot;text5&quot; onkeydown=&quot;return checkInput(event.keyCode,5)&quot;><br>
6:<input type=text name=&quot;text6&quot; onkeydown=&quot;return checkInput(event.keyCode,6)&quot;><br>
<input type=button name=&quot;text23&quot; onclick=&quot;sayHello(); return false&quot;>
<input type=image src=&quot;logo.gif&quot;>
</form>
</body>
</html>
</body>
</html>
 
also with the onkeydown event:

return checkInput(event.keyCode,1)
the 1 represents the index of the element in the form to move to next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top