PDA

View Full Version : Function does nothing when called?


shady
12-19-2002, 03:22 PM
Whilst I have experience of VB6 and HTML, I'm only just learning JavaScript, I've read various tutorials, but still am at a loss as to what I have done wrong here. I have declared a function in the head of my HTML doc as follows.

<html>
<head>
<script language="JavaScript">

function calculation()
{
var mySupplier = document.supplier.value

switch (mySupplier)

{
case "Amerada" :
alert ("Amerada");
break;

case "Crown" :
alert ("Crown");
break;

default :
alert ("Other");
break;
}

}

</script>
</head>

and I am attempting to call this function from a button which is defined in the body of the HTML doc thus:-

<input type="button" value="Calculate" onClick="calculation" >

but when i click the button, nothing at all happens. I have a drop down box called supplier, and I am simply trying to get the page to recognise which option has been selected, eventually I am going to place calculations in the SWITCH - CASE statements. I.E. depending on the option selected a specific calculation will be executed and the result will be displayed in another text box. As this is my first stab at JavaScript, I require as much help as anyone can give.

Regards

Shady

Spookster
12-19-2002, 04:08 PM
<input type="button" value="Calculate" onClick="calculation();" >

Skyzyx
12-19-2002, 04:14 PM
Well, you said you know HTML, so I'm assuming that you are aware that your <input> (or <select>) tag has to be inside a <form> tag.

To access the input (or select) you'd have to do something like this:


document.formName.inputName.value;


... in order for the script to know what's going on. If you're using IE, I'd suggest that you go into Internet Options, click on the Advanced Tab, and choose that you do want a notification about every script error. This should enable you the option to debug, and find out where the problem is. If you're using Mozilla or Netscape 6/7, type "javascript:" into the address bar to bring up your JavaScript debugger. This has helped me tremendously in fixing bugs in my code.

Hope this helps!