tags:

views:

279

answers:

2

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!

+2  A: 

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;
}
Jose Basilio
No luck, with this it doesn't even get to "out of loop", it just goes to the form's action page
Carlo
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
Carlo, please try my updated code. I think you will be very happy with the results.
Jose Basilio
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
+1  A: 

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.

Chad Birch
I tried this with my original javascript code and I still gotfalse true true true true true true true true out of loop
Carlo
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
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
Still no luck =/ I changed it to document.write(document.getElementById('radioButtonAnswer2') == null); document.write('<br />');and I still getfalsetruetruetruetruetruetruetruetrueout of loopIs there another function I could use?
Carlo
Yes, you can't use document.write either, it does the same thing. If you want to output it to the page directly, you need to do something like add a <div id="debug"></div> and then instead of document.write("something") you do document.getElementById('debug').innerHTML += "something". Hope that's clear, it's hard to write code without formatting in the comments.
Chad Birch
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