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"].ele ments["des cription<% =rowNum%>" ].focus();
window.location.hash="anch or";
-->
</script>
<a name="anchor"></a>
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"].ele
window.location.hash="anch
-->
</script>
<a name="anchor"></a>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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="anch or";
document.forms["form"].ele ments["des cription<% =rowNum%>" ].focus();
document.forms["form"].ele ments["des cription<% =rowNum%>" ].select() ;
-->
</script>
<a name="anchor"></a>
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="anch
document.forms["form"].ele
document.forms["form"].ele
-->
</script>
<a name="anchor"></a>
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="anch or";
document.forms[0].elements [0].focus( );
-->
</script>
<form style="margin-top:1000px">
<a name="anchor"></a>
<input type=text>
</form>
<script language="JavaScript">
<!--
window.location.hash="anch
document.forms[0].elements
-->
</script>
Check this:
<script language="JavaScript">
<!--
window.onload=function(){d ocument.fo rms["form" ].elements ["descript ion<%=rowN um%>"].foc us();}
window.location.hash="anch or";
-->
</script>
<a name="anchor"></a>
<script language="JavaScript">
<!--
window.onload=function(){d
window.location.hash="anch
-->
</script>
<a name="anchor"></a>
ASKER
I cannot use window.onload as I am using portlets.
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="anch or";
document.forms[0].elements [0].focus( );
-->
</script>
This works too:
<form style="margin-top:1000px">
<a name="anchor"></a>
<textarea></textarea>
</form>
<script language="JavaScript">
<!--
window.location.hash="anch
document.forms[0].elements
-->
</script>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Thanks for all your help
<!--
window.location.hash="anch
document.forms["form"].ele
-->
</script>
<a name="anchor"></a>