html - How to check input in Javascript for input type=text -


i trying fill table random numbers , have user complete totals. after entering totals wanted check answers, if answer correct wanted no longer allow input if answer wrong wanted cell highlighted , allow user try again.

the table filling , incorrect answers being identified further attempts modify answers don't give feedback. not sure why input button can't used multiple times guessing needs cleared accept entry. playing js simple haven't been able find working solution.

thanks

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"      "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">  <head> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <title>testing</title> <link href='https://fonts.googleapis.com/css?    family=open+sans:300italic,400italic,600italic,700italic,800italic,400,800,700,600,300'    rel='stylesheet' type='text/css'/>  <script type="text/javascript">  var ran = new array(4); var count = 0;  function randomarray ( ) {     count = 0;     var max = 10;     var min = 5;      ( var = 0; < ran.length; ++i)     {         ran[i] = math.floor(math.random()*(max - min + 1) + min);     }     iter = math.floor(math.random()*(4));     iter2 = math.floor(math.random()*(4));   }  function fillrandom(id) {      document.getelementbyid(id).innerhtml = ran[count];     count++;  }  function check(id, id2, i, j) {      var ans = document.getelementbyid(id).value.tostring();     var soln = document.getelementbyid(id2);       //document.getelementbyid(id2).style.border = "thick red double";     if (ans != (ran[i] + ran[j]))     {         soln.style.border = "thick red double";         correct = false;      }     else     {         soln.innerhtml = (ran[i] + ran[j]);         correct = true;       }  }  </script> </head>  <body> <h1>testing</h1>  <input type="submit" value="get questions" onclick="randomarray();fillrandom('num1');fillrandom('num2');fillrandom('num3');fillrandom('num4');"/> <p>try following questions:</p>  <table  id="data" border="thin black"> <tr>     <td>&nbsp;</td>     <td id="a">a</td>     <td id="b">b</td>     <td>total</td> </tr> <tr>     <td id="c">c</td>     <td id="num1">&nbsp;</td>     <td id="num2">&nbsp;</td>     <td id="t12"><input id="total_1_2" type="text" /></td> </tr> <tr>     <td id="d">d</td>     <td id="num3">&nbsp;</td>     <td id="num4">&nbsp;</td>     <td id="t34"><input id="total_3_4" type="text" /></td> </tr> <tr>     <td>total</td>     <td id="t13" ><input type="text" id="total_1_3"/></td>     <td id="t24"><input type="text" id="total_2_4"/></td>     <td id="t"><input type="text" id="total"/></td> </tr>  </table>   <br/> <input type="submit" value="check total" onclick="check('total_1_2', 't12', '0', '1');check('total_3_4', 't34', '2', '3');check('total_1_3', 't13', '0', '2');     check('total_2_4', 't24', '1', '3')">   </body>  </html> 

 function check(id, id2, i, j) {      var ans ;     if(document.getelementbyid(id) !== null)         ans = document.getelementbyid(id).value;     else         ans = document.getelementbyid(id2).textcontent;      var soln = document.getelementbyid(id2);      if (ans != (ran[i] + ran[j]))     {         soln.style.border = "thick red double";         correct = false;         }     else     {         soln.innerhtml = (ran[i] + ran[j]);          soln.style.border = "none";         correct = true;             }     } 

http://jsfiddle.net/g6zhl/


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -