0
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.
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by tapas.chand
0

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.

0

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?

0

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';
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.