tags:

views:

354

answers:

2

hi techies

i am using html inputbox , which is inside update panel i need the cursor to be set focused on that inputbox once the page is loaded so i called a function as follows

< body onload="javascript:document.Default.UserInputTextBox.focus();" >

but i getting the following Javascript error,

Microsoft JScript runtime error: 'document.Default.UserInputTextBox' is null or not an object

please help on this

+1  A: 

I do not think it is in the best interest of accessibility to dynamically alter a user's focus prior to alerting the user of such a change. What you can do that is accessible is redirect a user to a fragment URI pointing to the id attribute of the document object in question. That way you are not having to use any JavaScript.

So, for instance, if a user requests a page at this URI:
http://example.com/page.html

You can redirect the user with your ASP code and a HTTP 302 code to this page:
http://example.com/page.html#myInputIDValue

That would presume the input field in question had the following attribute:
id="myInputIDValue"

hi jjj < body onload="document.getElementById('UserInputTextBox').focus();" > is not working thanks for ur help
thanks for all who have answered it got worked when i called the function as follows<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate><input id="UserInputTextBox" type="text" size="75" maxlength="300" runat="server" /><script type="text/javascript" >window.onload=function(){document.getElementById('UserInputTextBox').focus();}</script></ContentTemplate></asp:UpdatePanel>
+1  A: 

You've got to wait for the DOM to be ready before trying to access elements in the dom; The onload event listener can wait for the entire document to be loaded including images and css... Which might be undesirable.

<head>
    <script type="text/javascript" charset="utf-8">
        // wait for the DOCUMENT to become ready.
        window.onload=function(){
          walkmydog()
        }
    </script>
</head>

Here's a detailed explanation for overcoming this sort of problem: http://www.javascriptkit.com/dhtmltutors/domready.shtml

A lot of people use javascript frameworks, like jQuery, MooTools, YUI, to overcome the browse incompatibility problems.

peterp