Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is My Code. I'm preparing a offline quiz system. This code shows multiple questions with there option field. Now in radio variable, currently it stores the option of question submitted by user who's name is q1. Since the name of every question set is dynamic,how can I store multiple values in radio variable ?
Like option of q1, q2... and so on ...
JavaScript
var currentPath = ((location+"").replace(/%20/g, " ").replace("file:///", "").replace("/", "\\").replace("index.html", ""));
var pad = currentPath+"\\quiz.mdb";
//var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;
var strConn = "Provider=microsoft.ace.oledb.12.0;Data Source=" + pad;
var cn = new ActiveXObject("ADODB.Connection");
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "SELECT * FROM ques_bank WHERE quizno = 'q_001'";
rs.Open(SQL, cn);
var sList = "<form target='sendinfo' id='infoform' onSubmit='return handleClick()'>";
while (!rs.EOF) {
    sList = sList +
    "<p>"+rs("question")+ "</p><br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='a' />"+rs("optionA")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='b' />"+rs("optionB")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='c' />"+rs("optionC")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='d' />"+rs("optionD")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='e' />"+rs("optionE")+"<br>"+
    "<input type='text' id='rank_list' name='q_id' value='"+ rs("ID") +"' /><br>"+
    "<hr>";
    rs.MoveNext();
}
document.write(sList+"<input type='submit' value='Submit'/></form>");

function test() {
}

//submit function
function handleClick() {
    var radios = document.getElementsByName("q1");
    var found = 1;
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
            alert(radios[i].value);
            found = 0;
            break;
        }
    }
    if(found == 1) {
        alert("Please Select Radio");
    }
    //event.preventDefault(); // disable normal form submit behavior
    return false; // prevent further bubbling of event
}

rs.Close();
cn.Close();
Posted
Updated 12-Jun-13 3:50am
v2

1 solution

As far as I understand, on "Submit Button" click, you need to loop through all the RadioButtons and check which ones are checked.

Solution
  1. Give a name to the form like below.
    HTML
    <form name="form1" target="sendinfo" id="infoform" onsubmit="return handleClick()"></form>
  2. Get all the input Elements of form like below.
    JavaScript
    var inputs = form1.elements;

  3. Find only the RadioButtons from those Elements.
    JavaScript
    var radios = [];
    
    //Loop and find only the Radios
    for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].type == 'radio') {
            radios.push(inputs[i]);
        }
    }

  4. Do whatever you want using radios.

Demo
[Demo] Get all RadioButtons inside the form[^]

Note
In the Demo, I have commented out the codes you used to break the on getting one Radio value just to show alert for every checked Radio value. You can use that as per your logic and requirements.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900