RSS Forums RSS
Please support our HTML and CSS advertiser: Lunarpages Web Hosting
Views: 8011 | Replies: 14
Reply
Join Date: Mar 2006
Location: Philippines
Posts: 112
Reputation: Covinus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Covinus's Avatar
Covinus Covinus is offline Offline
Junior Poster

Re: getting the text field value

  #11  
Aug 3rd, 2006
<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">


function operator(op){
if(op==0)
return "+"
if(op==1)
return "-"
if(op==2)
return "x"
else
return "/"
}
    function add(n1, n2, n3, n4){
    var ans
    if(n3==0){
    ans=n1+n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==1){
    ans=n1-n2
    if(ans==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==2){
    ans=n1*n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    if(n3==3){
    ans=n1/n2
    if(ans ==n4)
    alert("you got it right!")
    else
    alert("Sorry its wrong. the answer is: "+ans)
    }
    }
    
    
    
</script>
</head>
    <body>
    <font color="black" size="7"><center>
    <script type="text/javascript">
    
    
    var num1=(Math.floor(Math.random()*11))
    
    var num2=(Math.floor(Math.random()*11))
    
    var operation=(Math.floor(Math.random()*3))
    
    var convertedOp=operator(operation)
    
    document.write(num1 + "<br>" + convertedOp+ " <br>" + num2 + "<br>")
    document.write("______")
    var answer=document.form1.tfield.value;
    
    
    function check(){
    
    add(num1,num2,operation,answer)
    }
    </script>
    <form action="test.html" name="pormas">
    <input type="text" size=3 name="tfield" id="ans" value=0><br>
    <input type="button" value="Answer" name="ans" onclick="return check()">
    </form>
    
</body>
</html>    
this is my code for generating a number and do an operation randomly too. but i cant get the user input so i cant process it. it seems that the answer variable is always undefined.
Last edited by tgreer : Aug 3rd, 2006 at 11:29 am.
if you cant hit 2 birds with one stone try hitting just one or you will end up with nothing at all
Reply With Quote  
Join Date: Jul 2006
Location: Oblivion
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: getting the text field value

  #12  
Aug 3rd, 2006
we both have the same problem..getting the data from the text field inorder for us to compare it and generate it in a function..
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getting the text field value

  #13  
Aug 3rd, 2006
Number one READ what we are telling you.
1. PUT ; at the end of your statements.
2. replace var answer=document.form1.tfield.value; with what we told you THREE times now var answer=document.getElementById['tfield'].value

Now this line var answer=document.form1.tfield.value; is not only syntacticly wrong, it's in the wrong place. It will get parsed and executed by the browser BEFORE the text box "tfield" exists in the DOM (Document Object Model) so instead you need to move it into function Check()
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getting the text field value

  #14  
Aug 3rd, 2006
And you have this bit wrong

<input type="text" size=3 name="tfield" id="ans" value=0> it should be
<input type="text" size=3 name="tfield" id="tfield" value=0>

You can't reference the textbox by name, you must use the ID.

this code works
<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">


function operator(op){
if(op==0)
return "+";
if(op==1)
return "-";
if(op==2)
return "x";
else
return "/";
}
function add(n1, n2, n3, n4){
var ans;
if(n3==0){
ans=n1+n2;
if(ans ==n4)
alert("you got it right!");
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==1){
ans=n1-n2;
if(ans==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==2){
ans=n1*n2 ;
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans) ;
}
if(n3==3){
ans=n1/n2  ;
if(ans ==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
}



</script>
</head>
<body>
<font color="black" size="7"><center>
<script type="text/javascript">


var num1=(Math.floor(Math.random()*11));

var num2=(Math.floor(Math.random()*11));

var operation=(Math.floor(Math.random()*3));

var convertedOp=operator(operation);

document.write(num1 + "<br>" + convertedOp+ " <br>" + num2 + "<br>");
document.write("______");



function check(){
var answer=document.getElementById("tfield").value;
add(num1,num2,operation,answer) ;
}
</script>
<form action="test.html" name="pormas">
<input type="text" size=3 name="tfield" id="tfield" value=0><br>
<input type="button" value="Answer" name="ans" onclick="return check()">
</form>

</body>
</html> 
Last edited by hollystyles : Aug 3rd, 2006 at 7:08 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Mar 2006
Location: Philippines
Posts: 112
Reputation: Covinus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Covinus's Avatar
Covinus Covinus is offline Offline
Junior Poster

Re: getting the text field value

  #15  
Aug 3rd, 2006
sorry..

ok i get it
if you cant hit 2 birds with one stone try hitting just one or you will end up with nothing at all
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:44 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC