Link to home
Start Free TrialLog in
Avatar of inzaghi
inzaghi

asked on

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>
ASKER CERTIFIED SOLUTION
Avatar of smaccari
smaccari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smaccari
smaccari

<script language="JavaScript">
             <!--
              window.location.hash="anchor";  
              document.forms["form"].elements["description<%=rowNum%>"].focus();
           
 -->
            </script>
            <a name="anchor"></a>
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>
Avatar of inzaghi

ASKER

The focus and select do not work a intended, I dont think these can be used together with the window.location.hash
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>
Avatar of Zvonko
Check this:

             <script language="JavaScript">
             <!--
                window.onload=function(){document.forms["form"].elements["description<%=rowNum%>"].focus();}
                window.location.hash="anchor";
            -->
            </script>
            <a name="anchor"></a>
Avatar of inzaghi

ASKER

I cannot use window.onload as I am using portlets.
Avatar of inzaghi

ASKER

The element in question is a textarea. Does this make a difference?
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>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inzaghi

ASKER

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