0

I have two forms named "sum" and "sub". If i click on submit button of "sum" form, the function Calculate gets called, similarly if i click on submit button of "sub" form, the same function gets called. But i want that methods of "sum" form should not be executed on calling Calculate by "sub" form. I guess this can be done by if statement, can anyone please tell me the statement for doing this.

function Calculate() {
 var numsub=document.sub.subnum.value; //e.g: sub form assignment, this should not be executed on calling calculate from "sum" form

 totnumber=totnumber-parseInt(numsub);
 var sp1=document.sub.sp1.value;
 var tot1=sp1*numsub;
 totsellprice +=Math.floor(parseFloat(tot1));
 totnumsell =parseInt(numsub) + totnumsell;
 var number= document.sum.number.value;
 totnumber =parseInt(number) + totnumber;
 var sp= document.sum.sp.value;
 var total= sp*number;
 totprice +=Math.floor(parseFloat(total));
 avgbuy=totprice/totnumber;
 avgsell=totsellprice/totnumsell;
 status= "<h1>Share status</h1>Number of shares: " + totnumber + "</br>total money spent: " + totprice;
 status +="</br>Average Buying price: " + avgbuy;
  status= "</br>Number of shares sold: " + totnumsell + "</br>total money earned: " + totsellprice;
 status +="</br>Average Earning price: " + avgsell;
 document.getElementById('results2').innerHTML= status;
 }
1
  • when i run this by submitting "sum" form it says can't read property subnum of undefined. subnum is the property of "sub" form, I don't want it being read when submitting "sum" form. Commented May 18, 2013 at 5:13

2 Answers 2

1

function Calculate(type) {
 if(number=="sum")
  {
      //write the logic related to form1
      document.getElementById("sum").submit();
  }else{
      //write the logic related to form2
      document.getElementById("sub").submit();

   }

 }
Sign up to request clarification or add additional context in comments.

1 Comment

What is first? Your if syntax looks invalid.
0

pass a string to javascript function on submit. and check for the string value for sub and sum form respectively. Calculate('sum'); Calculate('sub') like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.