hi there,
i am using a sliding bar on my homepage.i want to set
focus on a textbox but
  document.getElementById("userid").focus() is givingerror,because textbox is invisible on page load.

is there any way to do this?

Thanks in advance.

How can u set focus to an element that is not visible in the html page.

either u have to make that textbox visible or just forget about making focus on it.

How can u set focus to an element that is not visible in the html page.

either u have to make that textbox visible or just forget about making focus on it.

Thank a lot for your reply, but that is what i was asking.
Is there any way to call a javascript function after the textbox comes into picture?

Thank a lot for your reply, but that is what i was asking.
Is there any way to call a javascript function after the textbox comes into picture?

Yes.
You wait for the onload event to occur. But in case that textbox doesn't have its display property set to 'none'. You can use something like:

onload = function(){ document.getElementById("userid").focus(); }

But, and if for some other reason this object doesn't recieve focus by the given command, than prior to calling focus on that element
you should try to do this:

document.getElementById("userid").tabIndex = 'auto';

Yes.
You wait for the onload event to occur. But in case that textbox doesn't have its display property set to 'none'. You can use something like:

onload = function(){ document.getElementById("userid").focus(); }

But, and if for some other reason this object doesn't recieve focus by the given command, than prior to calling focus on that element
you should try to do this:

document.getElementById("userid").tabIndex = 'auto';

Thank you for your reply.
As per your instructions i tried your code like

<script type="text/javascript">
    onload = function()
    {
        document.getElementById("userid").tabIndex = 'auto';
        document.getElementById("userid").focus();
    }
</script>

but it is still giving the error "Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."

Please help.

Yes.
You wait for the onload event to occur. But in case that textbox doesn't have its display property set to 'none'. You can use something like:

onload = function(){ document.getElementById("userid").focus(); }

But, and if for some other reason this object doesn't recieve focus by the given command, than prior to calling focus on that element
you should try to do this:

document.getElementById("userid").tabIndex = 'auto';

I got one solution for this.
I used setTimeout function and set the focus on the textbox on click of the open button of sliding bar as

onclick="window.setTimeout('setFocus()', 800)"

By 800 milisecs the textbox comes into picture and focus is set without any javascript error.
If u got any other solution, please provide. It will be quite a help.
Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.