0

I'm learning HTML, JavaScript & NodeJs (I'm just getting started(.

I'm trying to create a web site, which the user will answer some question (with radio buttons) and when the user click on Submit button, the answers will be sent to the server).

I'm creating the questions by this javascript function:

<script type="text/javascript" >

    var questionCounter = 0;
    var questionId = 0;

    function printQueue(question) {     

        questionId++;
        questionCounter++;  
        var radioName="Q"+questionCounter;
        var id = "Q" + questionCounter;                 
        document.write("<p> <b> " + question + " </b> </p>");                                                                                                                       
        document.write ("<input type=\"radio\" id=" + id + " name=" + radioName +
                            " onclick=\"check(this.value)\" value=\"5\">Best<br>");
        document.write ("<input type=\"radio\" id=" + id + " name=" + radioName + 
                                " onclick=\"check(this.value)\" value=\"4\">Very Good<br>");
        document.write ("<input type=\"radio\" id=" + id + " name=" + radioName + 
                                " onclick=\"check(this.value)\" value=\"3\">Good<br>");
        document.write ("<input type=\"radio\" id=" + id + " name=" + radioName +
                                " onclick=\"check(this.value)\" value=\"2\">Not Good<br>");
        document.write ("<input type=\"radio\" id=" + id + " name=" + radioName +
                                " onclick=\"check(this.value)\" value=\"1\">Worst<br>");
    }

</script>

and call this function in the following way:

<script type="text/javascript" >                
    printQueue("1.  XXX1 ? ");
    printQueue("2.  XXX2  ? ");
    printQueue("3.  XXX3  ? ");
    ...
    ...
    printQueue("N.  XXXN  ? "); 
</script>

Is there a way to go over the questions and get the value of the selected answer of each question ?

I looked at: How to get value of selected radio button

But it seem to cumbersome...

is there a way like:

1. loop over the questions
   1.1 for each Q getSelected().getvalue() 

Thanks

Community
  • 1
  • 1

1 Answers1

0

This might help

HTML

<form id="myForm">
    <div class='Q'> <span>Q1</span>
        <input type="radio" name="Q1" value="1" />1
        <br />
        <input type="radio" name="Q1" value="2" />2
        <br />
        <input type="radio" name="Q1" value="3" />3
        <br />
    </div>
    <div class='Q'> <span>Q2</span>
        <input type="radio" name="Q2" value="1" />1
        <br />
        <input type="radio" name="Q2" value="2" />2
        <br />
        <input type="radio" name="Q2" value="3" />3
        <br />
    </div>
    <input type='button' value='Get Values' id='temp'>
</form>

JS

$('#temp').on('click', function () {
    var Ans = [];
    $('.Q').each(function (index, Obj) {
        Ans.push({
            "Name ": $(this).find('span').text(),
            "Answer": $(this).find('input[type="radio"]:checked').val()
        })
    })
    console.log(Ans)
});

Demo

J Santosh
  • 3,808
  • 2
  • 22
  • 43