Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By glenngv

Thread: Radio Button On Second Page Prompts Error

  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Radio Button On Second Page Prompts Error

    I am a JavaScript/HTML dabbler and decided to create on online test for myself and a couple friends to help us study for a real estate class we're taking. My page works FANTASTIC in Internet Explorer. However, in Safari and Google Chrome, you can answer the first question and click next. Then, you can click the radio button for the answer page and click next, but it then triggers the error I coded to select an answer. How do I correct this? Your help is greatly appreciated. Attached is my coding.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script language="JavaScript">

    Questions = new Array(14);
    Questions[0] = {Q:"1. A professional opinion of a property's market value, based on established methods and using trained judgment, is performed by a",
    A: ["real estate attorney.","real estate appraiser.","real estate counselor.","home inspector."],
    V: [0,1,0,0] };
    Questions[1] = {Q:"ANSWER 1. A professional opinion of a property's market value, based on established methods and using trained judgment, is performed by a",
    A: ["real estate appraiser."],
    V: [0] };
    Questions[2] = {Q:"2. In general, when the supply of a certain commodity increases,",
    A: ["price tends to rise.","price tends to drop.","demand for it tends to rise.","demand for it tends to drop."],
    V: [0, 1, 0, 0] };
    Questions[3] = {Q:"ANSWER 2. In general, when the supply of a certain commodity increases,",
    A: ["price tends to drop."],
    V: [0] };
    Questions[4] = {Q:"3. Which factor primarily affects supply in the real estate market?",
    A: ["Population","Demographics","Employment","Governmental monetary policy"],
    V: [0, 0, 0, 1] };
    Questions[5] = {Q:"ANSWER 3. Which factor primarily affects supply in the real estate market?",
    A: ["Governmental monetary policy"],
    V: [0] };
    Questions[6] = {Q:"4. Which factor is MOST likely to influence demand for real estate?",
    A: ["Number of real estate professionals in the area","Number of full-time real estate professionals in the area","Wage levels and employment opportunities","Price of new homes being built in the area"],
    V: [0, 0, 1, 0] };
    Questions[7] = {Q:"ANSWER 4. Which factor is MOST likely to influence demand for real estate?",
    A: ["Wage levels and employment opportunities"],
    V: [0] };
    Questions[8] = {Q:"5. Property management, appraisal, financing, and development are all examples of",
    A: ["factors affecting demand.","specializations within the real estate industry.","non-real estate professions.","activities requiring broker management and supervision."],
    V: [0, 1, 0, 0] };
    Questions[9] = {Q:"ANSWER 5. Property management, appraisal, financing, and development are all examples of",
    A: ["specializations within the real estate industry."],
    V: [0] };
    Questions[10] = {Q:"6. A REALTOR is BEST described as an individual who is",
    A: ["a real estate professional who acts as a point of contact between two or more people in negotiating the sale, purchase, or rental of property.","any real estate professional who assists buyers, sellers, landlords, or tenants in any real estate transaction.","a member of the National Association of Real Estate Brokers who specializes in residential properties.","a real estate licensee and a member of the National Association of REALTORS (NAR)."],
    V: [0, 0, 0, 1] };
    Questions[11] = {Q:"ANSWER 6. A REALTOR is BEST described as an individual who is",
    A: ["a real estate licensee and a member of the National Association of REALTORS (NAR)."],
    V: [0] };
    Questions[12] = {Q:"7. A major manufacturer of automobiles announces that it will relocate one of its factories, along with 2,000 employees, to a small town. What effect will this announcement MOST likely have on the small town's housing market?",
    A: ["Houses will likely become less expensive.","Houses will likely become more expensive.","Because the announcement involves an issue of demographics, not of supply and demand, housing prices will stay the same.","The announcement involves an industrial property; residential housing will not be affected."],
    V: [0, 1, 0, 0] };
    Questions[13] = {Q:"ANSWER 7. A major manufacturer of automobiles announces that it will relocate one of its factories, along with 2,000 employees, to a small town. What effect will this announcement MOST likely have on the small town's housing market?",
    A: ["Houses will likely become more expensive."],
    V: [0] };
    CQ = 0;
    Answers = new Array(14);

    function getQAIndex() {
    idx= -1;
    for (i =0; i < document.Questionform.QA.length; i ++) {
    if (document.Questionform.QA[i].checked)
    idx = i;
    }
    return idx;
    }

    function CalcTotals() {
    idx = getQAIndex();
    if (idx == -1) {
    alert("Please select a value");
    return; }
    else
    StoreAnswers(idx);

    total = 0;
    for (i = 0; i < Answers.length; i++) {
    total = total +Answers[i];
    }
    h = '<font face="Arial" size="2"> Your score is: ' + total + ' out of 7<br><p><br></font>';


    h = h + "<br><center><input type='button' name='restart' value='Start Over' onClick='StartOver();'></center>"
    div = document.getElementById("QSection");
    div.innerHTML=h;

    }

    function StartOver() {
    CQ = 0;
    Answers = new Array(14);
    buildTable();

    }

    function StoreAnswers(idx) {
    Answers[CQ] = Questions[CQ].V[idx];
    }

    function doNext(){
    idx = getQAIndex();
    if (idx == -1)
    {
    alert("Please select a value");
    return; }
    else
    StoreAnswers(idx);
    CQ++;
    buildTable();
    }

    function doPrevious() {
    if (CQ > 0)
    {
    CQ--;
    buildTable();
    }
    }

    function buildTable() {

    h = "<font face=Arial><center><table>";
    h = h + "<tr><th colspan=2>" + Questions[CQ].Q + "</th></tr>";
    for (I = 0; I< Questions[CQ].A.length; I++){
    h = h + "<tr><td><input name='QA' type='Radio' + value='" + Questions[CQ].V[I] + "'></td>";
    h = h + "<td>" + Questions[CQ].A[I] +"</td><tr>";
    } // for i
    if (CQ < Questions.length -1) {
    h = h + "<tr><td align='center' colspan=2>";
    // if (CQ > 0)
    // h = h + "<input type='Button' name='NextButton' value='Next' onClick='doNext();'>";
    h = h + "<input type='Button' name='NextButton' value='Next' onClick='doNext();'>";
    }
    else
    h = h + "<tr><td align='center' colspan=2><input type='Button' name='CalcTotal' value='Calculate Results' onClick='CalcTotals();'>";

    h = h + "</table></center></font></form>";
    div = document.getElementById("QSection");
    div.innerHTML=h;
    }

    </script>



    <body bgcolor="#9999cc" onLoad="StartOver();">

    <div align="center">
    <p align="left"></p>
    <p align="left">&nbsp; </p>
    <div align="center">
    <center>
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="800">
    <tr>
    <td>
    <p align="center">
    <!--webbot bot="HTMLMarkup" startspan -->
    <form name='Questionform'>
    <div id="QSection" align="Center">
    <table>
    <tr></tr>
    </table>
    </div>
    </form>
    <!--webbot bot="HTMLMarkup" endspan i-checksum="34943" -->
    </p>
    </td>
    </tr>
    </table>
    </center>
    </div>



    </div>

    </body>

    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    <script language="JavaScript">
    Is that the trouble? <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
    Last edited by Philip M; 07-17-2014 at 09:12 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M View Post
    <script language="JavaScript">
    Is that the trouble? <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
    That's not the issue. The issue is when displaying the answer which has only one radio button, the document.Questionform.QA.length returns undefined in getQAIndex() function which then always returns -1. The function should be changed to this:
    Code:
    function getQAIndex() {
        var idx= -1;
        if (document.Questionform.QA.length) {
            for (var i =0; i < document.Questionform.QA.length; i ++) {
                if (document.Questionform.QA[i].checked) {
                    idx = i;
                }
            }
        }
        else if (document.Questionform.QA.checked) { //only one radio button
            idx = 0;
        }
        return idx;
    }
    dabbler411 likes this.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That was it! Thank you. Much appreciated.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •