0

I've got the following code, which writes radiobuttons through javascript to HTML

document.write('<input type="radio" name="AnswerA" />' + res.rows[jsonCounter][1]);

Say, I want to access this AnswerA field and find out if it's been selected, I can try :

    if(document.getElementById('AnswerA').checked) {
    }

But this doesn't seem to work - can anyone see what I'm doing wrong?

MattTheHack
  • 1,354
  • 7
  • 28
  • 48

2 Answers2

3

You need to set an id on the new DOM element for document.getElementById to use:

document.write('<input type="radio" name="AnswerA" id="AnswerA" />' + res.rows[jsonCounter][1]);
jim0thy
  • 2,095
  • 1
  • 18
  • 27
1

Add id in your DOM element. It will work.

document.write('<input type="radio" id="AnswerA" name="AnswerA" />' + res.rows[jsonCounter][1]);
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
Karan Patyal
  • 371
  • 3
  • 8