Function not defined error in javascript

Error: 'functionName' is not defined

Page 1 of 1

6 Replies - 5524 Views - Last Post: 25 March 2008 - 06:37 PM

#1 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Function not defined error in javascript

Posted 25 March 2008 - 01:44 PM

I am creating a form to calculate some figures and at the bottom I have a button to call the javascript function that performs the calculations and updates the text fields on the page. When the button is clicked however firefox gives me "Error: doMath is not defined" and IE gives me "Error: Object expected" on this line "<input type='button' name='calculate' value='Calculate' onclick='doMath()'>".

I've googled and searched through my javascript book but can't find a reason for it, I've never had a problem calling functions from a button before. If someone could please take a look, here is my code.

<html>
<head>
<title>Profit Analysis</title>
  
  <script language='javascript' type='javascript'>
	
	function doMath(){

	  int days = 30;
	  float ar = document.profit.ar.value;
	  float bg = document.profit.bg.value;
	  float sl = document.profit.sl.value;
	  float ep = document.profit.ep.value;
	  float et = document.profit.et.value;
	  float cs = document.profit.cs.value;
	  float liab = document.profit.liab.value;
	  float vand = document.profit.vand.value;
	  float vendorTheft = document.profit.vendorTheft.value;
	  float dailyTotal = ar + bg + sl + ep + et + cs + liab + vand + vendorTheft;
	  
	  float monthlyPayment = document.profit.monthlyPayment.value;
	  float monthlyProfit = monthlyTotal - monthlyPayment;
	  float yearlyProfit = monthlyProfit * 12;
	  float termProfit = monthlyProfit * 60;
	  
	  document.profit.dailyTotal = dailyTotal;
	  document.profit.mo_ar.value = ar * days;
	  document.profit.mo_bg.value = bg * days;
	  document.profit.mo_sl.value = sl * days;
	  document.profit.mo_ep.value = ep * days;
	  document.profit.mo_et.value = et * days;
	  document.profit.mo_cs.value = cs * days;
	  document.profit.mo_vand.value = vand * days;
	  document.profit.mo_vendorTheft = vendorTheft * days;
	  document.profit.montlyTotal.value = dailyTotal * 30;
	  
	  document.profit.monthlyProfit.value = monthlyProfit;
	  document.profit.yearlyProfit.value = yearlyProfit;
	  document.profit.termProfit.value = termProfit;
	}
	
  </script>
  
</head>

<body>
  <div><h1>Profit Analysis</h1>
	<form name='profit'>
	  <table>
		<tr>
		  <td>
			<table>
			  <tr>
				<td> </td>
				<td style='text-align:center'>Daily</td>
			  </tr>
			  <tr>
				<td><label for='ar'>Armed Robbery</label></td>
				<td><input type='text' name='ar'></td>
			  </tr>
			  <tr>
				<td><label for='bg'>Burglary</label></td>
				<td><input type='text' name='bg'></td>
			  </tr>
			  <tr>
				<td><label for='sl'>Shoplifting</label></td>
				<td><input type='text' name='sl'></td>
			  </tr>
			  <tr>
				<td><label for='ep'>Employee Productivity</label></td>
				<td><input type='text' name='ep'></td>
			  </tr>
			  <tr>
				<td><label for='et'>Employee Theft</label></td>
				<td><input type='text' name='et'></td>
			  </tr>
			  <tr>
				<td><label for='cs'>Customer Service</label></td>
				<td><input type='text' name='cs'></td>
			  </tr>
			  <tr>
				<td><label for='liab'>Liability</label></td>
				<td><input type='text' name='liab'></td>
			  </tr>
			  <tr>
				<td><label for='vand'>Vandalism</label></td>
				<td><input type='text' name='vand'></td>
			  </tr>
			  <tr>
				<td><label for='vendorTheft'>Vendor Theft</label></td>
				<td><input type='text' name='vendorTheft'></td>
			  </tr>
			  <tr>
				<td><label for='dailyTotal'>Total: </label></td>
				<td><input type='text' name='dailyTotal'></td>
			  </tr>
			</table>
		  </td>
		  <td> </td>
		  <td>
			<table>
			  <tr>
				<td> </td>
				<td style='text-align:center'>Monthly</td>
			  </tr>
				<td><label for='mo_ar'>Armed Robbery</label></td>
				<td><input type='text' name='mo_ar' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_bg'>Burglary</label></td>
				<td><input type='text' name='mo_bg' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_sl'>Shplifting</label></td>
				<td><input type='text' name='mo_sl' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_ep'>Employee Productivity</label></td>
				<td><input type='text' name='mo_ep' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_et'>Employee Theft</label></td>
				<td><input type='text' name='mo_et' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_cs'>Customer Service</label></td>
				<td><input type='text' name='mo_cs' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_liab'>Liability</label></td>
				<td><input type='text' name='mo_liab' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_vand'>Vandalism</label></td>
				<td><input type='text' name='mo_vand' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='mo_vendorTheft'>Vendor Theft</label></td>
				<td><input type='text' name='mo_VendorTheft' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='monthlyTotal'>Total: </label></td>
				<td><input type='text' name='monthlyTotal' disabled='true'></td>
			  </tr>
			</table>
		  </td>
		</tr>
		<tr><td> </td></tr>
		<tr>
		  <td>
			<table>
			  <tr>
				<td><label for='monthlyPayment'>Monthly Payment</label></td>
				<td><input type='text' name='monthlyPayment'></td>
			  </tr>
			  <tr>
				<td><label for='monthlyProfit'>Monthly Profit</label></td>
				<td><input type='text' name='monthlyProfit' disabled='true'></td>
			  </tr>
			  <tr>
				<td><label for='yearlyProfit'>Yearly Profit</label></td>
				<td><input type='text' name='yearlyProfit'></td>
			  </tr>
			  <tr>
				<td><label for='termProfit'>60 Months Profit</label></td>
				<td><input type='text' name='termProfit' disabled='true'></td>
			  </tr>			   
			</table>
		  </td>
		</tr>
	  </table>
	  <input type='button' name='calculate' value='Calculate' onclick='doMath()'>
	</form>
  </div>
</body>
</html>



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Function not defined error in javascript

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Function not defined error in javascript

Posted 25 March 2008 - 01:59 PM

Well, here are a couple of things:
  • Line 35, you have an incorrect spelling of 'monthlyTotal'
  • Your opening script tag should look like this: <script type='text/javascript'>, the language attribute is deprecated.
  • Line 22, monthlyTotal isn't defined.

Have fun :)
Was This Post Helpful? 0
  • +
  • -

#3 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: Function not defined error in javascript

Posted 25 March 2008 - 02:15 PM

View Postthehat, on 25 Mar, 2008 - 01:59 PM, said:

Well, here are a couple of things:
  • Line 35, you have an incorrect spelling of 'monthlyTotal'
  • Your opening script tag should look like this: <script type='text/javascript'>, the language attribute is deprecated.
  • Line 22, monthlyTotal isn't defined.
Have fun :)


Thanks for pointing that out, i've been pouring over the code for the last hour and missed those. They didn't correct my problem though but I did figure out what it was. I was declaring the floats incorrectly, i should have done it like this:

var dailyTotal = parseFloat(document.profit.dailyTotal.value);

Thanks for the help!

This post has been edited by bstonehill: 25 March 2008 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Function not defined error in javascript

Posted 25 March 2008 - 02:31 PM

Oh yeah, I forgot to mention that. Don't use int or float, just use var for all of them. Try this:

<script type="text/javascript">
    
    function doMath(){
      var days = 30;
      var ar = document.profit.ar.value;
      var bg = document.profit.bg.value;
      var sl = document.profit.sl.value;
      var ep = document.profit.ep.value;
      var et = document.profit.et.value;
      var cs = document.profit.cs.value;
      var liab = document.profit.liab.value;
      var vand = document.profit.vand.value;
      var vendorTheft = document.profit.vendorTheft.value;
      var dailyTotal = ar + bg + sl + ep + et + cs + liab + vand + vendorTheft;
      
      var monthlyPayment = document.profit.monthlyPayment.value;
      var monthlyProfit = document.profit.monthlyTotal.value - monthlyPayment;
      var yearlyProfit = monthlyProfit * 12;
      var termProfit = monthlyProfit * 60;       
      
      document.profit.dailyTotal = dailyTotal;
      document.profit.mo_ar.value = ar * days;
      document.profit.mo_bg.value = bg * days;
      document.profit.mo_sl.value = sl * days;
      document.profit.mo_ep.value = ep * days;
      document.profit.mo_et.value = et * days;
      document.profit.mo_cs.value = cs * days;
      document.profit.mo_vand.value = vand * days;
      document.profit.mo_vendorTheft = vendorTheft * days;
      document.profit.monthlyTotal.value = dailyTotal * 30;
	
	  
      document.profit.monthlyProfit.value = monthlyProfit;
      document.profit.yearlyProfit.value = yearlyProfit;
      document.profit.termProfit.value = termProfit;
    }
    
  </script>


This post has been edited by thehat: 25 March 2008 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Function not defined error in javascript

Posted 25 March 2008 - 02:31 PM

Ive been using Javascript for a very long time, and have never declared a data type for a variable, Im not even 100% positive you can do it. Here is how your script should look


function doMath()
{
  var days = 30;
  var ar = document.profit.ar.value;
  var bg = document.profit.bg.value;
  var sl = document.profit.sl.value;
  var ep = document.profit.ep.value;
  var et = document.profit.et.value;
  var cs = document.profit.cs.value;
  var liab = document.profit.liab.value;
  var vand = document.profit.vand.value;
  var vendorTheft = document.profit.vendorTheft.value;
  var dailyTotal = eval(ar + bg + sl + ep + et + cs + liab + vand + vendorTheft);
  
  var monthlyPayment = document.profit.monthlyPayment.value;
  var monthlyProfit = eval(onthlyTotal - monthlyPayment);
  var yearlyProfit = eval(monthlyProfit * 12);
  var termProfit = eval(monthlyProfit * 60);
  
  document.profit.dailyTotal = dailyTotal;
  document.profit.mo_ar.value = eval(ar * days);
  document.profit.mo_bg.value = eval(bg * days);
  document.profit.mo_sl.value = eval(sl * days);
  document.profit.mo_ep.value = eval(ep * days);
  document.profit.mo_et.value = eval(et * days);
  document.profit.mo_cs.value = eval(cs * days);
  document.profit.mo_vand.value = eval(vand * days);
  document.profit.mo_vendorTheft = eval(vendorTheft * days);
  document.profit.montlyTotal.value = eval(dailyTotal * 30);
  
  document.profit.monthlyProfit.value = monthlyProfit;
  document.profit.yearlyProfit.value = yearlyProfit;
  document.profit.termProfit.value = termProfit;
}    



Notice I got rid of your data types, you dont need those and in fact I think they're the cause of your errors. Also, I use eval() where I have calculations occurring.
Was This Post Helpful? 0
  • +
  • -

#6 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Function not defined error in javascript

Posted 25 March 2008 - 02:32 PM

Snap, lol.
Was This Post Helpful? 0
  • +
  • -

#7 hexeliebe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-March 08

Re: Function not defined error in javascript

Posted 25 March 2008 - 06:37 PM

Although you have the correct answer already, I just wanted to point out a very hard lesson I had to learn when I started programming at the age of 35. It's something I train other programmers to take to heart their first day of training.

And the lesson is, if you don't find the answer within 15 minutes of looking at the code, walk away. Take a break, have a cigaretter, cigar or blonde and then come back with fresh eyes.

Some of my best programming has been done at 3 a.m. laying in bed thinking over a problem I couldn't solve during working hours.

View PostPsychoCoder, on 25 Mar, 2008 - 02:31 PM, said:

Ive been using Javascript for a very long time, and have never declared a data type for a variable, Im not even 100% positive you can do it. Here is how your script should look


function doMath()
{
  var days = 30;
  var ar = document.profit.ar.value;
  var bg = document.profit.bg.value;
  var sl = document.profit.sl.value;
  var ep = document.profit.ep.value;
  var et = document.profit.et.value;
  var cs = document.profit.cs.value;
  var liab = document.profit.liab.value;
  var vand = document.profit.vand.value;
  var vendorTheft = document.profit.vendorTheft.value;
  var dailyTotal = eval(ar + bg + sl + ep + et + cs + liab + vand + vendorTheft);
  
  var monthlyPayment = document.profit.monthlyPayment.value;
  var monthlyProfit = eval(onthlyTotal - monthlyPayment);
  var yearlyProfit = eval(monthlyProfit * 12);
  var termProfit = eval(monthlyProfit * 60);
  
  document.profit.dailyTotal = dailyTotal;
  document.profit.mo_ar.value = eval(ar * days);
  document.profit.mo_bg.value = eval(bg * days);
  document.profit.mo_sl.value = eval(sl * days);
  document.profit.mo_ep.value = eval(ep * days);
  document.profit.mo_et.value = eval(et * days);
  document.profit.mo_cs.value = eval(cs * days);
  document.profit.mo_vand.value = eval(vand * days);
  document.profit.mo_vendorTheft = eval(vendorTheft * days);
  document.profit.montlyTotal.value = eval(dailyTotal * 30);
  
  document.profit.monthlyProfit.value = monthlyProfit;
  document.profit.yearlyProfit.value = yearlyProfit;
  document.profit.termProfit.value = termProfit;
}    



Notice I got rid of your data types, you dont need those and in fact I think they're the cause of your errors. Also, I use eval() where I have calculations occurring.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1