简体   繁体   中英

I can't get the property value to print to textarea in JavaScript, I get an error: Uncaught TypeError: Cannot read property 'value' of null

I am struggling to get the value to print to text-area for my online exam application. I am new to JavaScript but am learning fast. I would welcome any help as it is driving me crazy trying all different way but without success. Thanks in advance.

Html code:


<!DOCTYPE html>

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
         <title>testexam</title>
             <link rel="stylesheet" href="tutor.css" media="all" type="text/css">               
                         <!-- Custom Fonts -->
             <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
             <!-- JS code from stack overflow; modified to suit application by Paul Gillis.-->
             <script type="text/javascript"></script>
</head> 
      <body>
           <div class= "container">
                <div class="col-sm-4">
                      <img class="img- img-responsive img-center" src="img/EXAM.PNG" alt="">
                </div>  
          </div>
  <h1>Exam Level 1</h1>
      <form name ="testexam">
          <ol>
                <div><img class="img- img-responsive img-center" src="examimg/A1sharp.png" alt=""></div>
                     <li>Question 1, which is the correct answer</li>   
        <ul> 
            <li><input type = "radio" name ="q1" value = "wrong" >This note is B</input></li>
            <li><input type = "radio" name ="q1" value = "correct" >This note is A#</input></li>
            <li><input type = "radio" name ="q1" value = "wrong" >This note is C</input></li>
        </ul>
            <hr>
  <div><img class="img- img-responsive img-center" src="examimg/A1.png" alt=""></div>           
       <li> Question 2, which is the correct answer</li>
           <ul>
               <li><input type = "radio" name ="q2" value = "correct">This note is A</input></li>
                   <li><input type = "radio" name ="q2" value = "wrong">This note is B</input></li>
                       <li><input type = "radio" name ="q2" value = "wrong">This note is C</input></li>
           </ul>
                <hr>
    <div><img class="img- img-responsive img-center" src="examimg/B1.png" alt=""></div>
      <li> Question 3, which is the correct answer</li>
        <ul>
            <li><input type = "radio" name ="q3" value = "wrong">This note is D</input></li>
            <li><input type = "radio" name ="q3" value = "correct">This note is B</input></li>
            <li><input type = "radio" name ="q3" value = "wrong">This note is A</input></li>
        </ul>
            <hr>
                <input type ="button" value = "hello" onclick="sayhello()"></input>
                <input type ="button" value = "submit" onclick ="checkAll()"></input>
                <input type ="reset" value = "clear"></input>
                    <textarea rows ="6" cols = "60" name ="answersBox">Your Answer Results are Listed   Below</textarea>
</body>
    <footer>
        <script type="text/javascript" src="js/testexam.js"></script>
    </footer>
</html>

Javacript code

function sayhello()
   {
       alert("hello");
   }
var text2display = "";
    var answers = new Array(3);
        answers[0] = "1.This note is A#\n";
        answers[1] = "2.This note is A\n";
        answers[2] = "3.This note is B\n";


var questions = new Array(3);
    questions[0] = "q1";
    questions[1] = "q2";
    questions[2] = "q3";


function checkQs(s)
{
    var qs = document.getElementsByName(s);
        var noOfRadios = qs.length;

            for(var i = 0; i < noOfRadios; i++)
    {
              if(qs[i].checked)
        {
                  if (qs[i].value == "correct")
                      text2display = text2display + "Well Done You are Correct";
                          else text2diaplay = text2display + answers[questions.indexOf(s)];

            break;      
        }       
    }
}
   function checkAll()
{
   for(var i = 0; i < questions.length; i++)
{
     checkQs(questions[i]);
}
        testexam.answerBox.value = text2display;
}

you have given wrong text area name:use this:you text area name is answersBox . and you need to close your form tag also.your HTML is not well organised try to follow some tutorials.

testexam.answersBox.value = text2display;

  function sayhello() { alert("hello"); } var text2display = ""; var answers = new Array(3); answers[0] = "1.This note is A#\\n"; answers[1] = "2.This note is A\\n"; answers[2] = "3.This note is B\\n"; var questions = new Array(3); questions[0] = "q1"; questions[1] = "q2"; questions[2] = "q3"; function checkQs(s) { var qs = document.getElementsByName(s); var noOfRadios = qs.length; for(var i = 0; i < noOfRadios; i++) { if(qs[i].checked) { if (qs[i].value == "correct") text2display = text2display + "Well Done You are Correct"; else text2diaplay = text2display + answers[questions.indexOf(s)]; break; } } } function checkAll() { for(var i = 0; i < questions.length; i++) { checkQs(questions[i]); } testexam.answersBox.value = text2display; } 
 <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>testexam</title> <link rel="stylesheet" href="tutor.css" media="all" type="text/css"> <!-- Custom Fonts --> <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <!-- JS code from stack overflow; modified to suit application by Paul Gillis.--> <script type="text/javascript"></script> </head> <body> <div class= "container"> <div class="col-sm-4"> <img class="img- img-responsive img-center" src="img/EXAM.PNG" alt=""> </div> </div> <h1>Exam Level 1</h1> <form name ="testexam"> <ol> <div><img class="img- img-responsive img-center" src="examimg/A1sharp.png" alt=""></div> <li>Question 1, which is the correct answer</li> <ul> <li><input type = "radio" name ="q1" value = "wrong" >This note is B</input></li> <li><input type = "radio" name ="q1" value = "correct" >This note is A#</input></li> <li><input type = "radio" name ="q1" value = "wrong" >This note is C</input></li> </ul> <div><img class="img- img-responsive img-center" src="examimg/A1.png" alt=""></div> <li> Question 2, which is the correct answer</li> <ul> <li><input type = "radio" name ="q2" value = "correct">This note is A</input></li> <li><input type = "radio" name ="q2" value = "wrong">This note is B</input></li> <li><input type = "radio" name ="q2" value = "wrong">This note is C</input></li> </ul> <div><img class="img- img-responsive img-center" src="examimg/B1.png" alt=""></div> <li> Question 3, which is the correct answer</li> <ul> <li><input type = "radio" name ="q3" value = "wrong">This note is D</input></li> <li><input type = "radio" name ="q3" value = "correct">This note is B</input></li> <li><input type = "radio" name ="q3" value = "wrong">This note is A</input></li> </ul> </ol> <input type ="button" value = "hello" onclick="sayhello()"></input> <input type ="button" value = "submit" onclick ="checkAll()"></input> <input type ="reset" value = "clear"></input> <textarea rows ="6" cols = "60" name ="answersBox">Your Answer Results are Listed Below</textarea> </form> </body> <footer> <script type="text/javascript"> </script> </footer> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM