Advertisement

08.19.2008 at 04:30AM PDT, ID: 23659150 | Points: 20
[x]
Attachment Details

Looping through numerous divs doesn't stop?

Asked by hmcgeehan in JavaScript

Hi
I'm using Javascript to loop through a set amount of divs.
My page has a load of divs (see code snippet) that show questions and answers
i.e.
a1
q1
a2
q2 etc...

Now I try to loop through them all with my code ....

function showAnswers() {
                  
                  var i=1;
                  var question = document.getElementById("q"+i);

                  while (question != null)
                  {
                        alert(i);
                        i=i+1;
                  }


            }

This does show an alert but it doesn't stop! It just keeps going.
Any ideas how to ensure it stops when there's no more divs on the page?

Thanks
H

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
<div class="faq">
			FREQUENTLY ASKED QUESTIONS
			</div>
			<div id="q1" class="question" onclick="showAnswers();">
				Q: What are the ...			</div>
			<div id="a1" class="answer">
	 			blah 			</div>
			<div id="q2" class="question">
				Q: Difference between ...			</div>
			<div id="a2" class="answer">
				blah ....			</div>
 
Loading Advertisement...
 
[+][-]08.19.2008 at 04:34AM PDT, ID: 22258762

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.19.2008 at 04:43AM PDT, ID: 22258839

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.19.2008 at 05:05AM PDT, ID: 22258980

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628