Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is element visible?

Status
Not open for further replies.

cfgoodies

Programmer
Sep 9, 1999
16
US
I've got a form that has fields inside of <div> tags.
------------------------------------
<div id=&quot;set_1&quot; style=&quot;display:none&quot;>
<input type=&quot;text&quot; name=&quot;foo&quot; />
</div>

<div id=&quot;set_2&quot;>
<input type=&quot;text&quot; name=&quot;bar&quot; />
</div>
------------------------------------
How can I bring focus to the first &quot;visible&quot; form field?

Thanks in advance.
 
Loop thru the form.elements.array checking the display value, the first time you encounter what you want - or !none, set focus ,then break out of the loop - done.

[bb]
 
I've tried this, but it doesn't work.

for (a = 1; a <= document.wiz.elements.length; a++) {
if (document.wiz.elements[a].style.display != '') {
document.wiz.elements[a].focus();
break;
}
}

I get the error: 'document.wiz.elements[...].style' is not an object.

Thanks for your help.
 
Check you have the Form tag in there, and it is named correctly - and try document.forms[&quot;wiz&quot;] too if that still won't work.


for (var a = 1; a <= document.wiz.elements.length; a++) {
if (document.wiz.elements[a].style.display != 'none') {
document.wiz.elements[a].focus();
break;
}
}

[bb]
 
I tried both and couldn't get it to work. The questionable code is below. I took out everything that was irrelevant. I'm executing this code in IE 5.5 if that makes a difference. Again, I really appreciate your help.

<html>
<head>
<title>Test</title>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function begin() {
set_1.style.display = 'none';
for (a = 0; a <= document.wiz.elements.length; a++) {
if (document.forms[&quot;wiz&quot;].elements[a].style.display != 'none') {
document.forms[&quot;wiz&quot;].elements[a].focus();
break;
}
}
}
</script>
</head>
<body onload=&quot;Javascript:begin();&quot;>
<form name=&quot;wiz&quot; action=&quot;blank.cfm&quot; method=&quot;post&quot;>

<div id=&quot;set_1&quot;>
<!-- Show this option later -->
Option: <input type=&quot;text&quot; name=&quot;option&quot; />
</div>

<div id=&quot;set_2&quot;>
Name: <input type=&quot;text&quot; name=&quot;name&quot; />
</div>

<div id=&quot;set_3&quot;>
Email: <input type=&quot;text&quot; name=&quot;name&quot; />
</div>

</form>

</body>
</html>
 
It's because you have set the display on the div containing the input, instead of the form element.

Forget about the divs [and NS4.x] - and just hide the actual elements.

Then to hide the text too - access it as the previousSibling of the input.

Or you can carefully label the form elements and divs to match your loop - and hide them that way.

<bb>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top