MattTheHack
MattTheHack

Reputation: 1384

How to access elements written by javascript to DOM

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?

Upvotes: 0

Views: 82

Answers (2)

Karan Patyal
Karan Patyal

Reputation: 381

Add id in your DOM element. It will work.

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

Upvotes: 1

jim0thy
jim0thy

Reputation: 2155

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]);

Upvotes: 3

Related Questions