Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with recalc 4

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi,

I have three checkboxes, worth 40,40, and 20. I wish to return a total to a textbox, based on which are checked. Here is the JS routine:

function recalculate(f) {
alert("here");
var total=0;
(if f.elements["chk2006"].checked){
total+=40;
}
(if f.elements["chk2005"].checked){
total+=40;
}
(if f.elements["chk2004"].checked){
total+=20;
}
f.elements["txtTotal"]=total;
return;
{

The form name is "f". Here is one of the checkboxes:
<input type="checkbox" name="chk2004" ></Input>

And here is a button to call the routine:
Input type="button" value="recalculate" onclick="recalculate()" />

When I click the button, I do not see the alert box. And the error is:
Line 124: (the line of the button)
Char: 1
Error: Object expected
Code: 0


I had originally tried calling the routine direct from clicking on the checkboxes themselves but the same error would occur for three lines (the three lines where the checkboxes are.

I don't understand what else to try. Help!

THanks,
KB

 
just at a glance id say the syntax on your if statements looks screwy the condition goes inside of the parenthesis "()" the if declaration goes outside

Code:
function recalculate(f) {
alert("here");
  var total=0;
  if(f.elements["chk2006"].checked){
    total+=40;
}
  if(f.elements["chk2005"].checked){
    total+=40;
}
  if(f.elements["chk2004"].checked){
    total+=20;
}
f.elements["txtTotal"]=total;
return;
{

MCP, .Net Solutions Development <%_%>
 
Your function is supposed to receive a value f, but in the function call you pass no parameters. The if statements will cause errors, too, as werD420 stated.

Also, inputs don't have </input> closing tags. And I hope your button input has an opening < that isn't there in your original example.

Lee
 
Thanks, serves me right for trying to work at 2:00 am.

I update the code, and it's "almost there":

<Script language="JavaScript" type="text/javascript">

function recalculate() {
alert("here");
var total=0;
if (f.elements["chk2006"].checked){
total+=40;
}
if (f.elements["chk2005"].checked){
total+=40;
}
if (f.elements["chk2004"].checked){
total+=20;
}

f.elements["txtTotal"]==total;

}
</Script>


I get no errors but that last line does not get the total into txtTotal. Perhaps it has to do with getting a number to act like text?? The text box just stays empty.

Thanks,
KB
 
scorpio has posted the answer, but I'll elaborate:

double equals signs (==) checks for equality in javascript, and a single equals sign (=) assigns a value. So your line:
Code:
f.elements["txtTotal"][!]==[/!]total
checks for equality between the value stored in total, and the value stored in the textbox txtTotal. Since they do not match it returns the value false, but the return value is lost because you are not assigning it to anything. So the solution: remove one of the equals signs.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Thanks all - everything works perfect now.

Troll - the f is the name of the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top