Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ok, this situation is a little weird but anyway. This PHP code generates several radiobuttons:

    for($i = 0; $i<count($questionList); $i++)
    {
    	echo $questionList[$i]->__get(QuestionId).'-'.$questionList[$i]->__get(QuestionText).'<br />';

    	$answerList = $questionList[$i]->GetAnswers();

    	for($j = 0; $j<count($answerList); $j++)
    	{
    		echo '<br /><input type=\'radio\' name=\'group'.$i.'\' id=\'radioButtonAnswer'.$answerList[$j]->__get(AnswerId).'\' value=\''.$answerList[$j]->__get(AnswerId).'\' >'.
    		$answerList[$j]->__get(AnswerText).'</input>';
    	}
    	echo '<br /><br />';
    }

Ok, that works fine, after the checkboxes are created, I'm trying to run some code to get all the radio buttons and it didn't work, so I tried just getting one radio button several times, and it only gets it the first time.

function Validate()
{

	var i = 1;

	do
	{
		document.writeln(document.getElementById('radioButtonAnswer2') == null);

		i ++;
	}while(i < 10);

	document.writeln('out of loop');

	return false;
}

So I know FOR SURE that 'radioButtonAnswer2' exists and it shouldn't be null. But this is what I get when I click the submit button:

false true true true true true true true true out of loop

The first time is not null, but after that, it is. Any thoughts?

Thanks!

share|improve this question
    
Can you add a snippet of the HTML code that is output by the PHP? Just want to take a look at that to make sure it's all coming out properly. –  Chad Birch May 15 '09 at 5:09
    
Sure here's some of it <br /><br /><input type="radio" name="group0" id="radioButtonAnswer4" value="4" /><label for="radioButtonAnswer4">Answer4</label><br /><input type="radio" name="group0" id="radioButtonAnswer1" value="1" /><label for="radioButtonAnswer1">Answer1</label><br /><input type="radio" name="group0" id="radioButtonAnswer2" value="2" /><label for="radioButtonAnswer2">Answer2</label><br /><input type="radio" name="group0" id="radioButtonAnswer3" value="3" /><label for="radioButtonAnswer3">Answer3</label> –  Carlo May 15 '09 at 5:12
    
Answer updated, found the cause. –  Chad Birch May 15 '09 at 5:24
add comment

2 Answers

up vote 1 down vote accepted

It may be because your HTML you are generating is invalid. You also shouldn't be explicitly calling the __get() function, but that's an unrelated issue most likely.

Something like:

<input type="radio" ...>Label Text</input>

is not the correct way to define a radio button.

Try this code:

for($j = 0; $j<count($answerList); $j++)
{
        echo '<br /><input type="radio" name="group'.$i.'" id="radioButtonAnswer'.$answerList[$j]->AnswerId.'" value="'.$answerList[$j]->AnswerId.'" />';
        echo '<label for="radioButtonAnswer'.$answerList[$j]->AnswerId.'">'.$answerList[$j]->AnswerText.'</label>';
}


Edited to add: Ah, now I see. You're using document.writeln(). That function overwrites the content of the page.

So the first time into the loop, the element does exist, and it does a document.writeln() call, which writes "true" to the page. This overwrites everything that was on the page before (didn't you notice how when the page loads, it only has the output of the javascript?). The next time through the loop, it tries to look for the radio button again, but it's been erased and replaced with the javascript output. Now it no longer exists.

share|improve this answer
    
I tried this with my original javascript code and I still got false true true true true true true true true out of loop –  Carlo May 15 '09 at 5:03
    
Hmm, I'll look for other problems, but you should use this version instead of what you had, regardless. Anywhere else in your code that you're doing similar things should also be fixed up. –  Chad Birch May 15 '09 at 5:07
    
Thanks for the suggestion. It does look like a better practice and makes the code look better. Thanks a lot for the help too, I thought validating the answers in my survey would be very easy! –  Carlo May 15 '09 at 5:08
    
Still no luck =/ I changed it to document.write(document.getElementById('radioButtonAnswer2') == null); document.write('<br />'); and I still get false true true true true true true true true out of loop Is there another function I could use? –  Carlo May 15 '09 at 5:35
1  
Haha oh god, this was quite something! I thought writeln was like Console.WriteLine() in C# or something, you know, to save you adding the <br />. This solved my problem, thanks a lot! –  Carlo May 15 '09 at 5:40
show 1 more comment

You can use document.getElementsByName("group") to get all the radio buttons.

This loop works fine. The problems is with document.writeln(), it replaces the html in the page and therefore the DOM elements are gone. Here's an updated version using an Alert instead.

function Validate(){  
  var radioGroup = document.getElementsByName("group");
  var results = "";
  for(i = 0, len = radioGroup.length; i < len; i++){
    currentRadio = radioGroup[i];
    results += "\n" + currentRadio.id + " is ";
    results += (currentRadio.checked ? "checked" : "not checked");
  }
  results += "\n..out of loop...";
  alert(results);     
  return false;
}
share|improve this answer
    
No luck, with this it doesn't even get to "out of loop", it just goes to the form's action page –  Carlo May 15 '09 at 4:47
    
It's crazy, I added an extra line to your function right after "var radioGroup =" to check if the radioGroup var was null, and it isn't!, so the loop should work, but it doesn't even get in it, look: function validate() { var radioGroup = document.getElementsByName("group1"); document.write('is null: ' + radioGroup == null) for(i = 0, len = radioGroup.length; i < len; i++) { document.write(i + ' times in loop');//this doesnt show document.writeln(radioGroup[i].checked); } document.writeln("out of loop"); return false; } –  Carlo May 15 '09 at 4:58
    
Carlo, please try my updated code. I think you will be very happy with the results. –  Jose Basilio May 15 '09 at 5:29
1  
Hey this worked too! I got distracted with the other answer! I wish I could mark them both as right, thank you very much Jose! –  Carlo May 15 '09 at 5:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.