...

View Full Version : Missing ) after condition?



flare1028us
06-23-2004, 12:16 AM
Hey all

I have been asked to write a js prog/util for an after-school program at my church. It's pretty simple: just read the problem, type in the answer,
and click a button to see if it's right.

My problem is this:

I have written the basic structure for this prog the way I always have,
except now it's not working. I ran it through the netscape javascript console
and I got the following error:


function eval()
{
if (document.ans.2t2.value="4");
alert("Correct");
}


Missing ) after condition.


I cannot see what is wrong with this script.
I feel kind of dumb b/c conditons are usually very easy and I never have
problems with them.

Here is my full souce:


<html>
<head>
<script language="javascript">

function eval()
{
if (document.ans.2t2.value =="4") ;
alert("2x2=4. Correct.");
}

</script>
</head>
<body>
<form name="ans">
2x2 <input type="textarea" name="2t2"><br>
<input type="button" value="Eval" onclick="eval()"></form>
</body>
</html>

Can anyone see what is wrong with this script?
Any help is appreciated.
Carl

Willy Duitt
06-23-2004, 12:24 AM
Names or ID's can not begin with a numeric. Change your input name and do not use eval as a function name since there is already a javascript eval method and therefore it is a reserved word.

Oakendin
06-23-2004, 01:30 AM
function eval() {
if (document.ans.2t2.value="4");
alert("Correct");
}



Get rid of the ";".

So that it looks like (With Willy's suggestion)


function evaluateAnswer() {
if (document.ans.2t2.value="4")
alert("Correct");
}

glenngv
06-23-2004, 03:12 AM
You should use the conditional operator == not the equality operator =
And if you still want to name your field starting with numbers, use the bracket notation (http://www.litotes.demon.co.uk/js_info/sq_brackets.html).



function evaluateAnswer() {
if (document.ans.elements['2t2'].value=="4")
alert("Correct");
}

flare1028us
06-23-2004, 03:13 AM
Thankx 4 the help

lol now I just feel dumb! :)

Willy Duitt
06-23-2004, 09:14 AM
And if you still want to name your field starting with numbers, use the bracket notation (http://www.litotes.demon.co.uk/js_info/sq_brackets.html).

I was not aware of that. :p

Thanks;
.....Willy

glenngv
06-23-2004, 09:26 AM
I was not aware of that. :p

Thanks;
.....Willy
You should be by now. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum