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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on loop

Status
Not open for further replies.

TheCandyman

Technical User
Sep 9, 2002
761
US
I'm trying to loop though a few Div's and set them to hidden/visible. I have tried this a few ways but can't seem to get a winning solution as both of my try's show errors.


In the body:
Code:
 <div id="Group1" style="display:none;">
    <p>Question 1</p>
    <p>Question 2</p>
    <p>Question 3</p>
</div>
...
...
<div id="Group6" style="display:none;">
   <p>Question 7</p>
   <p>Question 7</p>
   <p>Question 7</p>
</div>




In the head, try #1:
Code:
for (i=1; i<=7; i++)
	{
	object = document.getElementById('Group' + i);
	object.style.display = 'none';
	}
Error: object is null
object.style.display = 'none';




In the head, try #2:
Code:
for (i=1; i<=7; i++)
	{
	document.getElementById('Group'+i).style.display= 'none';
	}
Error: document.getElementById("Group" + i) is null
document.getElementById('Group'+i).style.display= 'none';




If i do this it works, but it can get pretty large so i'd rather do the loops above:
Code:
document.getElementById('Group1').style.display= 'none';
document.getElementById('Group2').style.display= 'none';
document.getElementById('Group3').style.display= 'none';
document.getElementById('Group4').style.display= 'none';
document.getElementById('Group5').style.display= 'none';
document.getElementById('Group6').style.display= 'none';
 
Why is your loop going up to 7 when you only have 6 groups?

That could cause a null object, as there is no Group7.

Also you won;t see any change at all, since your divs have their display set to none so they are not show, and your loop does the same thing. Perhaps you want to set them as block so they are shown.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Good point on the loop. I take a peak at that.

They reason I won't see any changes is because i have a link that shows the groups when clicked, but needed to cycle though all of them and make sure they are hidden first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top