...

View Full Version : onclick and function problems



julianflynn
12-16-2007, 12:17 PM
I'm new to JavaScript programming and have to do a course in it for a surveying degree. I've worked out the logic and it works fine but when it comes to getting the button to call on the function it wont work. I get an error message saying "Object doesn't support this property or method"

I've called the function:

function calcChange(fmCalc) {
// write your code here
var totalOwes = document.fmCalc.txtTotal;
var totalPaid = document.fmCalc.txtCash;
var totalChange = totalPaid - totalOwes;
totalChange = totalChange.toFixed(2)


if (totalChange < 0)
{
alert ("You still owe $" + Math.abs(totalChange))
}

and the button is


<input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(this.Form)"/></td>


when I change the button's onclick to

<input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(fmCalc)"/></td>

it wont work either and comes up with the same error message.

I've tried everything in my textbook but all the examples pretty much show that the part after "onclick" in the button's code has to be the same as the functions name and it should work. I have no idea what is wrong with it and would greatly appreciate any help. I've attached the txt document so it can be seen what I am trying to do.
Thanks

Philip M
12-16-2007, 12:36 PM
var totalOwes = document.fmCalc.txtTotal.value;
var totalPaid = document.fmCalc.txtCash.value;

var totalChange = totalPaid - totalOwes;
totalChange = totalChange.toFixed(2);

if (totalChange < 0) {
alert ("You still owe $" + Math.abs(totalChange));
}

else {
alert (totalChange + " Change");
}

document.write should be avoided - it erases the current page and creates a new one. In any case your syntax was wrong.

oesxyl
12-16-2007, 12:41 PM
function calcChange(fmCalc) {
// write your code here
var totalOwes = document.fmCalc.txtTotal;
var totalPaid = document.fmCalc.txtCash;
var totalChange = totalPaid - totalOwes;
totalChange = totalChange.toFixed(2)


if (totalChange < 0)
{
alert ("You still owe $" + Math.abs(totalChange))
}



- you must close the red bracket
- is not an error but you don't use anyware the argument fmCalc of the function
- check to see if the value of totalOwes and totalPaid are corect after click, you can use alert for that

best regards

Kor
12-16-2007, 01:02 PM
but the real mistake is that the argument fmCalc is used as a name in the function (and even so, not in a correct way), while it has been passed as an object (and even so not in a correct way - this.Form).

A lot of other errors (1. the function it is not closed, 2. when subtracting, the values must be parsed into decimal numbers, 3. you must have forgot that toFixed() turns a number into a string thus you can not use Math methods upon it, 4. there is of no use to perform a Math.abs into an alert...... and so on...)
Try this:


function calcChange(f) {
var totalOwes = Number(f['txtTotal'].value);
var totalPaid = Number(f['txtCash'].value);
var totalChange = totalPaid-totalOwes;
totalChange = totalChange.toFixed(2);
if (Number(totalChange)<0){
alert ("You still owe $ " + totalChange);
}
}
...
...
<input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(this.form)"/>

Philip M
12-16-2007, 01:17 PM
3. you must have forgot that toFixed() turns a number into a string thus you can not use Math methods upon it,



I don't agree.

alert ("You still owe $" + Math.abs(totalChange))

returns the correct amount.

<script type = "text/javascript">
x = 2.3456789;
y = x.toFixed(2);
alert (y + " " + typeof y);
z = Math.ceil(y);
alert (z + " " + typeof z);
</script>

But I agree that there are quite a lot of errors (especially logic errors) other than those I mentioned.

Kor
12-16-2007, 01:32 PM
Maybe I was vague when said "Math methods", while I should have been said "math anything, mainly math operators like + and -".

Some math operators needs decimal numbers, some don't. Isn't safer to use always decimal numbers when you do math calculations, and strings in the rest of cases?

toFixed() transforms a number into a string, that is out of any doubt. What costs you to make it a number when you decide that you need a "math something"?

What a bout the rest of my notes?

technica
12-16-2007, 02:41 PM
If u have put the text box in any form then try to check the form name and the formname used in the javascript code.

Generally for this the text bx should be kept inside a form.

Philip M
12-16-2007, 04:58 PM
Maybe I was vague when said "Math methods", while I should have been said "math anything, mainly math operators like + and -".


<script type = "text/javascript">
x = 2.3456789;
y = x.toFixed(2);
alert (y + " " + typeof y); // 2.35 string
z = y-1;
alert (z + " " + typeof z); // 1.35 number
</script>

In fact only + causes problems, because of course 2+2 as a string concatenates to 22.

I have no quarrel with your other points. My post was intended to help julianflynn to get his script to work, although it contains quite a lot of other errors which need correcting, as you say.

julianflynn
12-16-2007, 08:39 PM
Wow, I got rid of all the "document.write" and changed them to
"document.txt2dollar.value = twoDollarCoins"
There are probably as you all say a lot of errors with naming and syntax but the program seems to work ok now. Thanks for the number of responses. I've gone through and fixed up as much as possible. Thank you so much I have been stuck for days.
I'm a newbie but i'm getting there slowly.
Thanks again to all who replied.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum