Member Avatar
jpknoob

Hi all, i seem to be having an issue with a calculate function i have made,. the script works fine in all browsers except Firefox! I was hoping someone could lend a hand and explain what I'm doing wrong.

The JS:

<code>
<script type="text/javascript">
//Calculator formulae
function multiply(){
Form.result.value = ((form.inputA.value * form.inputB.value * form.inputC.value * 150) / 1000000) ;
</code>

The form;

<code>
<form id="Form" action="">
<fieldset>
<input type="text" name="inputA" value="" />
<input type="text" name="inputB" value="" />
<input type="text" name="inputC" value="" />
<input type="button" name="calc" value="Calculate" onclick="multiply()"/>
<input type="text" name="result" value="" />
</fieldset>
</form>
</code>

looks like you are missing an ending bracket }, you are also not closing your script tag.

Member Avatar
jpknoob

Thanks for your response, but this is only a snip of my code, the original code has all the correct jazz, just not working in FF.

Edit;

The JS:

<code>
<script type="text/javascript">
//Calculator formulae
function multiply(){
Form.result.value = ((form.inputA.value * form.inputB.value * form.inputC.value * 150) / 1000000) ;
}
</script>
</code>

The form;

<code>
<form id="Form" action="">
<fieldset>
<input type="text" name="inputA" value="" />
<input type="text" name="inputB" value="" />
<input type="text" name="inputC" value="" />
<input type="button" name="calc" value="Calculate" onclick="multiply()"/>
<input type="text" name="result" value="" />
</fieldset>
</form>
</code>

do you have
<code>

</code>

in your file, or are they supposed to suggest code tags on this forum?

does it give any result, does it give an javascript error, ... ?

Member Avatar
jpknoob

The code tags were supposed to be forum tags, another typo, d'oh!

The code runs perfectly in all browsers except FF, when i hit the calculate button, nothing happens.

try adding an alert into your function, just to check whether it starts running the function or not.

I would suggest using the id attribute instead of the name attribute and using document.getElementById in your javascript.

try this:

<script type="text/javascript">
//Calculator formulae
function multiply(){
document.Form.result.value = ((form.inputA.value * form.inputB.value * form.inputC.value * 150) / 1000000) ;
}
</script>

actually... javascript is case sensitive so since you named your form 'Form'
you need to reference 'Form' not 'form'. anyway... try this.

<script type="text/javascript">
//Calculator formulae
function multiply(){
document.Form.result.value = ((document.Form.inputA.value * document.Form.inputB.value * document.Form.inputC.value * 150) / 1000000) ;
}
</script>
Member Avatar
jpknoob

Have tried the "document.Form.result.value..." but had no luck. i will give the "document.getElementById" a go, can anyone recommend any good tutorials for this method? I'm new to java script

It's easier to pass a reference to the form element from the onclick handler.

This avoids having to discover the form in the DOM with document.all , document.getElementById() , document.forms[] etc.

function multiply(form){
	form.result.value = ((form.inputA.value * form.inputB.value * form.inputC.value * 150) / 1000000) ;
}
<form action="">
	<fieldset>
		<input type="text" name="inputA" value="" />
		<input type="text" name="inputB" value="" />
		<input type="text" name="inputC" value="" />
		<input type="button" name="calc" value="Calculate" onclick="multiply(this.fom)"/>
		<input type="text" name="result" value="" />
	</fieldset>
</form>

Airshow

In function multiply(), I changed your 'form' ID to capitalized 'Form'. That made your code work in Iceweasel (Firefox), Konqueror, Epiphany and Opera on Debian. Epiphany very usefully printed the error on stderr.

For tracing and debugging JS, download http://getfirebug.com/. It works well for me (I have a 10k line JS program written from scratch; Firebug makes it easy to debug the program in Iceweasel/Firefox.) It told me exactly what the problem was.

For all you'd want to know about JS, get David Flanagan's "Javascript the Definitive Guide" (O'Reilly, ISBN 0-596-10199-6). I'm still using the 5th edition (1996); it's what I used to learn JS.

Member Avatar
jpknoob

Thank you very much for the feedback and advice, i will explore these and get back with an update, hopefully a solution!

Try your code with: <!doctype native> instead of whatever doctype you are using currently
and you're a go!

Hi Troy, <!doctype native> sounds fascinating. Do you have a reference for it please?

Airshow

Member Avatar
jpknoob

Hello all and happy new year!

I tried Troy III recommendation and it worked! I have never heard of doctype native and am still bemused as to why this works. I was using strict doctype before. Thanks all for the help/advice

Member Avatar
jpknoob

Just an update, changing the doctype resulted in non validation from w3, so i reverted back to an earlier comment to use the document.getElementById and managed to get it to work. For those interested, the javascript code looks like;

document.getElementById("result").value = ((document.getElementById("inputA").value * document.getElementById("inputB").value * document.getElementById("inputC").value * 150) / 1000000);

and in the HTML, i change the name to id.

Thanks all for the help