4 Replies - 497 Views - Last Post: 17 February 2013 - 04:16 AM

#1 cirustus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-February 13

function error function undefined

Posted 16 February 2013 - 08:41 PM

i was trying to get this code working but it keeps comming up with an error
<html>

<head>



</head>

<script language="Javascript" type="text/javascript">
var add, minus, divide, multiply;
function initialise() 
{
add      = document.calculateForm.operator.options[0];
minus    = document.calculateForm.operator.options[1];
multiply   = document.calculateForm.operator.options[2];
divide = document.calculateForm.operator.options[3];

document.calculateForm.txt1.value = 0;
document.calculateForm.txt2.value = 0;
}
function script1()
{
var ans = 0;
var x = parseInt(document.calculateForm.txt1.value);
var y = parseInt(document.calculateForm.txt2.value);

if (1.selected) ans = x + y;
alert (+ ans);
else if (2.selected) ans = x - y;
alert (+ ans);
else if (3.selected) ans = x * y;
alert (+ ans);
else (4.selected) ans = x / y;
alert (+ ans);
}
</script>
<body>
<body onload="initialise()">
This will be a basic calculator hope you enjoy it woman :P/>/>
<p>
<form action="post" name="calculateForm" id="calculateForm">
<input type="text" value="txt1" name="txt1" size="4" id="txt1">
<select name="operator">
<option value="add">+</option>
<option value="minus">-</option>
<option value="multiply">x</option>
<option value="divide">/</option>
</select>
<input type="text" value="txt2" name="txt2" size="4" id="txt2">
<p>
<input type ="button" value ="calculate" onclick ="script1();"/>
</form>
</body>

</html>


Is This A Good Question/Topic? 0
  • +

Replies To: function error function undefined

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: function error function undefined

Posted 16 February 2013 - 09:17 PM

Please remember to post exact details about errors, include the actual error messages, just as they appear. Inexact error descriptions tend to be pretty much useless.

One thing in there stands out, though. The IF-ELSE clause starting on line 27. What exactly is 1.select, 2.select, etc... supposed to be doing? Are you trying to get the selected option somehow?

You can get the selected value of a <select> box by just accessing the "value" property of the select box itself. No need to check each individual option.
<select id="mySelect">
    <option value="add">+</option>
    <!-- etc... -->
</select>
<button id="myButton">Click Me</button>

<script type="text/javscript">
var selectBox = document.getElementById("mySelect");
var button = document.getElementById("myButton");

button.onclick = function() {
    var selectedValue = selectBox.value;
    if (selectedValue == "add") {
        alert("You've chosen to add stuff!");
    }
    else if (selectedValue == "etc...") {
        // etc...
    }
}
</script>


See what I mean?

Also, note how I am accessing the select and button elements in that code. Instead of going through the whole document.formname.element thing, I just assign the elements "id" attributes, and then use those to access the elements in Javascript. This is far less trouble, especially when it comes to more advanced markup.

Finally, also note how I am setting the button.onclick property. This is a better way to set event callbacks on elements, instead of using the "onclick" attribute in the HTML itself. Ideally you don't want any Javascript code in the actual HTML. This lets you do the Javascript in the Javascript, not the HTML. (Believe me, this will start making a huge difference when the code starts to grow.) - There is a lot more to event handling in Javascript though. You may want to look into that! This is a good place to start:
Introduction to Events (quirksmode.org)
Was This Post Helpful? 0
  • +
  • -

#3 cirustus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-February 13

Re: function error function undefined

Posted 16 February 2013 - 09:27 PM

the error i was getting is Line: 38
Error: The value of the property 'initialise' is null or undefined, not a Function object

that would come up when i opened the html file and im lost as to where i went wrong for this error to happen
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: function error function undefined

Posted 17 February 2013 - 01:34 AM

a somewhat weird message, though the reason is most probably a syntax error. the fact is, if there is a syntax error in your script, the script itself is discarded and any call to that script (e.g. via an onload attribute) fails because the called function does not exist.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,393
  • Joined: 12-December 12

Re: function error function undefined

Posted 17 February 2013 - 04:16 AM

This line
if (1.selected) ans = x + y;

is the main error in your code, as suggested by Atli.

Also:
language="Javascript" is obsolete - delete it;
Your script occurs between the head and body tags - it should be within one or the other;
You have two body tags.

And a forms method would be post or get.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1