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

javascript set focus 2

Status
Not open for further replies.

Alcar

Programmer
Sep 10, 2001
595
US
It sounds simple and it should be done in a blink of an eye...

form
table1
table2
table3
table4
table5
table6
textbox1
textbox2
textbox3
textbox4

When my page loads I just would love to have the focus set to textbox1. I have already set the tab index properties correctly... but that doesn't seem to do the trick and the textbox gets the focus after 3 or 4 times I hit the Tab, and as my client put it: "it's a worthless piece of crap".

So here I come and I create a little javascript to get it going:

<BODY onload=&quot;javascript:form1.table6.texbox1.blur&quot;>

or even setfocus

result:
form1.table6.texbox1 is null or is not a valid object

what next?

(tough morning, frying brain)
Daren J. Lahey
Just another computer guy...
If you are unsure of forum etiquette check here FAQ796-2540
 
Try this:

In the code behind on the page add an attribute:

PageLoad
txtbox1.Attributes(&quot;onblur&quot;) = &quot;javascript:SetFocustxt1();&quot;

in the HTML of the page:
function SetFocustxt1() {
document.PageName.txtbox1.focus();
}

Happy coding!

Regards,
Natalee
 
Another syntax for the code behind is:

txtbox1.Attributes.Add(&quot;onblur&quot;, &quot;javascript:SetFocustxt1();&quot;

either way should work
 
guys isn't onblur when the box loses focus? If so that script will never let you click on any other box.
Not up on my javascript so i may be wrong. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
or you could do this:

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)

Dim s As String
s = &quot;<SCRIPT language='javascript'>document.getElementById('&quot; & ctrl.ID & &quot;').focus() </SCRIPT>&quot;

RegisterStartupScript(&quot;focus&quot;, s)

End Sub

this way you can set the focus to whatever control you want, just pass in the control to the sub
i.e.
SetFocus(txtUserName)

D'Arcy
 
Nice one, D'Arcy.
penny1.gif
penny1.gif
 
you da man big D! Daren J. Lahey
Just another computer guy...
If you are unsure of forum etiquette check here FAQ796-2540
 
btw you might want to FAQ that one D... (when ever you have time)

peace out Daren J. Lahey
Just another computer guy...
If you are unsure of forum etiquette check here FAQ796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top