Bold Text Here

The included file is a code to make a quiz using javascript and radio buttons .
When you finally submit , it shoudl print the question numbers for the correct answer in green and the wrong answers in red .
But the code is not working properly.
NEED HELP!

Hi rapidwein,

your code has a lot of errors, in the javascript and also in the html.

On the html:

  • There's a lot of

<

p> tags that has no closing tag. You shold remove all of those, because they are not used and might will cause problem because the syntax is invalid.
* Some divs have # on the ID, remove them.
* All radio buttons have the same name, this means that you can only select of radio button. The names should be equal per question, so you can select one radio for each question.

The javascript i already fixed the syntax, use this:

function Init()
{
    var arrDivs = new Array('1','2','3','4','5','6','7','8','9','10');
    for(var j=0;j<10;j++)
    {
        var chosenDiv=document.getElementById(arrDivs[j]);
        chosenDiv.style.display='block';
    }
}

function DisplayDiv()
{
    $("#btnSubmit").click(function() { 
        var arrcolor = new Array();

        for(var j=0;j<10;j++)
        {
            if($("'#'+arrDivs[j] input:radio:checked").val()==document.getElementById("true"))
                arrcolor[j]=1;
            else
                arrcolor[j]=0;
        }

        var arrnewDiv = new Array('31','32','33','34','35','36','37','38','39','40');
        for(var k=31;k<41;k++)
        {
            if(arrcolor[k-31]==0)
            {
                document.getElementById(arrnewDiv[k-31]).style.color="red";
                document.getElementById(arrnewDiv[k-31]).style.display='block';
            }
            else
            {
                document.getElementById(arrnewDiv[k-31]).style.color="green";
                document.getElementById(arrnewDiv[k-31]).style.display='block';
            }
        }
    });
}

Hope this helps.

If i am gonna give different names to each group , then the code gets too big . i have to manually access each group to get selected value . This is too cumbersome . Is there an easier method?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.