Joshua Java

Focus to first enabled field

Posted on: June 11, 2007

Recently on my project I’ve got an assignment at my office to have a function that will focus to first enabled field on an HTML form. I will post it here for my own archive, just in case I need it some other time😛. I hope it will be useful for you too.

<c:choose>
	<c:when test="${form.mode eq 'NEWMODE'}" >
		<c:set var="disabled" value="true"/>
	</c:when>
	<c:when test="${form.mode eq 'VIEWMODE'}" >
		<c:set var="disabled" value="true"/>
	</c:when>
	<c:otherwise>
		<c:set var="disabled" value="false"/>
	</c:otherwise>
</c:choose>
<html:text property="name" maxlength="8" size="10" 
	disabled="${disabled}" />

// This is the javascript function:
var forms = document.forms[0];
var i = 0;

// Let’s scan over the whole elements on the form
for(i=0; i < forms.elements.length; i++){ // Go only to non hidden field if( forms.elements[i].type != 'hidden' ){ // Focus to elements that's not disabled if(! forms.elements[i].disabled ){ forms.elements[i].focus(); break; }else{ continue; } } }[/sourcecode] * I'm using struts here, and the code snippets is for the JSP page.😉

1 Response to "Focus to first enabled field"

i think i know this function😀
beware of lots of mode josh…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: