On 21 Feb, 18:57, Little_Grungy <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a form definition for two radio buttons named choices. I want
> to loop through the radio buttons to perform a check with javascript
> in the rendered html page, however due to the radio button _id's that
> are rendered by the template, I cannot access them with javascript
> using getElementById(). Wondering if anyone can suggest how to solve
> this?
>
> My form definition contains:
>
> choices = forms.ChoiceField(label='Choices', required=True,
> widget=forms.RadioSelect, choices=options)
> options = (('good', 'Good'), ('bad', 'Bad'))
>
> The rendered html is as follows:
>
> <th>
>   <label for="id_choices_0">Choices:</label>
> </th>
> <td>
> <ul>
>   <li>
>     <label>
>       <input id="id_choices_0" type="radio" name="choices"
> value="good"/>
>         Good
>      </label>
>   </li>
>   <li>
>     <label>
>       <input id="id_choices_1" type="radio" name="choices" value="bad"/
>
>         Bad
>     </label>
>   </li>
> </ul>
>
> My Javascript function is passed an object obj which contains a
> choices paramater. obj.choices will be either "good" or "bad" so I
> would normally be able to set it based on what is passed. My normal
> approach would be to check the current setting by using
> getElementById, then comparing to what is passed from obj.choices and
> if equal, setting the relevant radio.
>
> As I have two id's id_choices_0 and id_choices_1 I'm guessing that I
> can't use id_choices_0 in getElementById as in previous worlds. Before
> Django my radio buttons would only have one id which I would use to
> reference.
>
>  for (var i=0; i < document.getElementById('id_choices_0').length; i++)
> {
>   if (document.getElementById(id_choices_[i]).value == obj.choices)
> document.getElementById(id_choices_[i]).checked = true;
>  }
>
> Can anyone help?

It's not Django that's the problem, it's the javascript. Why not try
something like this (untested):

var choice;
var choice_length = 2;
for (var i=0; i < choice_length; i++)
{
  choice = document.getElementById('id_choices_' + i);
  if (choice.value == obj.choices)
  {
     choice.checked = true;
  }
}

--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to