0

I have an array containing two objects:

var questions = [{question: "Is the sky blue?", choices: ["Yes", "No"], correctAnswer:0},{question: "Is water wet?", choices: ["Yes", "No"], correctAnswer:0}]

I have some javascript to make the questions render on the screen with HTML:

<script>
 function askQuestion(x){
  var questiontest = document.getElementById('question');
  questiontest.innerHTML = x.question;
 }

 function loop(){ 
  for(i = 0; i < questions.length; i++){
  askQuestion(questions[i]);
  }
 }

 left some stuff out here, not that relevant to question

 addEventListener('load',loop);
</script>

My HTML looks like this, displays the current question but not the text of the choices found in the questions object:

<label for="choice" id="question"></label>
<br><input type="radio" id="choice_1" name="choice" value="1"></input>
<br><input type="radio" id="choice_2" name="choice" value="2"></input>

Using this code I can render the question and then two radio buttons i.e. without the text of the choices. Is there anyway that I can render the text of the choices from the questions object next to the radion buttons? or do I have to do something stupid like this to make it render correctly?

<br><input type="radio" name="choice" value="1"><p id="choice_1"></p></input>

I'm trying to do it with vanilla javascript at the moment and will research doing in with jQuery shortly.

Thanks any help appreciated!

2
  • A label will be rendered next to the input element if grouped correctly <label><input type="radio" name="choice" value="1" />This is the text next to the radio button</label> Commented Jan 14, 2014 at 11:41
  • The problem is that the text needs to be dynamic i.e. I could put <label><input type="radio" name="choice" value="1" />Is the sky blue?</label> but then when the second question is found by the loop function that text will remain... unless I am mistaken? Commented Jan 14, 2014 at 11:44

1 Answer 1

2

Re-structure your HTML so you can label the inputs individually, then it becomes easy

HTML

<form id="qform" action="">
    <fieldset>
        <legend id="l0"></legend>
        <label id="l1" for="c1"></label>
        <input id="c1" type="radio" name="choice" value="1" />
        <br />
        <label id="l2" for="c2"></label>
        <input id="c2" type="radio" name="choice" value="2" />
    </fieldset>
</form>

JavaScript

function questionFactory() {
    var i = 0,
        l0 = document.getElementById('l0'),
        l1 = document.getElementById('l1'),
        l2 = document.getElementById('l2');
    return function askQuestion() {
        if (i >= questions.length) return false;
        l0.textContent = questions[i].question;
        l1.selected = false;
        l1.textContent = questions[i].choices[0];
        l2.selected = false;
        l2.textContent = questions[i].choices[1];
        ++i;
        return true;
    }
}
var ask = questionFactory();
ask(); // asks q1, returns true
ask(); // asks q2, returns true
ask(); // returns false, there were no more questions to ask

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.