user2394553
user2394553

Reputation: 1

Handling two forms through single javascript function

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;
 }

Upvotes: 0

Views: 89

Answers (2)

PSR
PSR

Reputation: 40358

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();

   }

 }

Upvotes: 1

Rahul11
Rahul11

Reputation: 264

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.

Upvotes: 0

Related Questions