setting the focus on a field element

I am using javascript window.location.hash to scroll to a position in a page where I set the anchor on page refresh.
The page is scrolling to the position correctly but the cursor does not focus on the element i have specified in the code below.

Any suggestions? Thanks in advance

              <script language="JavaScript">
              <!--
                document.forms["form"].elements["description<%=rowNum%>"].focus();
                window.location.hash="anchor";
            -->
            </script>
            <a name="anchor"></a>
inzaghiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smaccariCommented:
Maybe you should just do this (put the focus method after the location.hash):

<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms["form"].elements["description<%=rowNum%>"].focus();
            -->
            </script>
            <a name="anchor"></a>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smaccariCommented:
<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms["form"].elements["description<%=rowNum%>"].focus();
           
 -->
            </script>
            <a name="anchor"></a>
0
smaccariCommented:
Oops sorry bad post :p

I just wanted to add a select method on the field, to be sure to see the cursor inside it:

<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms["form"].elements["description<%=rowNum%>"].focus();
           document.forms["form"].elements["description<%=rowNum%>"].select();
 -->
            </script>
            <a name="anchor"></a>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

inzaghiAuthor Commented:
The focus and select do not work a intended, I dont think these can be used together with the window.location.hash
0
smaccariCommented:
This code works fine for me:

<form style="margin-top:1000px">

            <a name="anchor"></a>
<input type=text>
</form>
<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms[0].elements[0].focus();
              -->
</script>
0
ZvonkoSystems architectCommented:
Check this:

             <script language="JavaScript">
             <!--
                window.onload=function(){document.forms["form"].elements["description<%=rowNum%>"].focus();}
                window.location.hash="anchor";
            -->
            </script>
            <a name="anchor"></a>
0
inzaghiAuthor Commented:
I cannot use window.onload as I am using portlets.
0
inzaghiAuthor Commented:
The element in question is a textarea. Does this make a difference?
0
smaccariCommented:
No it don't.
This works too:

<form style="margin-top:1000px">

            <a name="anchor"></a>
<textarea></textarea>
</form>
<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms[0].elements[0].focus();
              -->
</script>
0
ZvonkoSystems architectCommented:
Portlets or no portlets, you can access that form element first AFTER the element is rendered on the page.
If you cannot use onload (I see not why), then place the original script at the end of the page, or at least after the textarea.

0
inzaghiAuthor Commented:
As the code is executing in a loop, I have called setTimeOut with a delay of 100ms before settign the focus and this works.

Thanks for all your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.