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,346
  • 7
  • 26
  • 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
  • 1,975
  • 1
  • 17
  • 25
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,049
  • 9
  • 43
  • 72
Karan Patyal
  • 361
  • 3
  • 8