Help needed with Javascript function

J

John

Hi all,
I would welcome any assistance with this problem.

I have an Input Textbox named 'givendiscount'
I have a drop down box named 'd1'

I have an Input Textbox named 'subtotalvalue'
I have an Input Textbox named 'discountvalue'
I have an Input Textbox named 'totalcostvalue'

What should happen is that a user enters a discount amount in 'givendiscount'
He/She then chooses an option from 'd1'

After clicking a button (the code says onClick="calculatecost()") then three values should be
entered into the boxes.

Here is the function...

<script language="javascript">
function calculatecost() {
var totalcost,discount,subtotal,ind;
discount=+(document.forms[0].givendiscount.value);
ind=+(document.forms[0].d1.options.selectedIndex);
subtotal=+(document.forms[0].d1.options.[ind].value);
totalcost=subtotal-(subtotal/discount};
document.forms[0].subtotalvalue.value=subtotal;
document.forms[0].discountvalue.value=discount;
document.forms[0].totalcostvalue.value=totalcost.toFixed(2);
}
</script>

The code is in the HEAD section of the page.

Trouble is, nothing is happening at all...not even any error codes.
Can anyone help please.

TIA.
 
K

Kevin Spencer

There are several funky things about your script:
discount=+(document.forms[0].givendiscount.value);

First of all, what does "=+" mean? This appears throughout your code. There
is no such operator in JavaScript.

Second, the value property of a form element is always a string. When you
add strings together, you get longer strings, not the result of adding
numbers together:

"1" + "2" = "12"
1 + 2 = 3

JavaScript uses variants. A variant is a variable data type that can hold
any data type. It can hold a number. It can hold a string. But a number is
not a string. When you assign a string to a variant, the variant is holding
a string. Use the JavaScript parseInt() function to convert it to a number.
And be sure it *can* be parsed as a number. Parsing "A" as a number, for
example, will yield the value NaN ("Not a Number").

Oddly enough, you can assign a number to a form element, and it magically
becomes a string. Go figure. But you have to know these things in order to
use them properly.
ind=+(document.forms[0].d1.options.selectedIndex);

Aside from the non-existent operator, you're looking at the "selectedIndex"
property of the "options" array of a "select" object. There is no such
property of the "options" array. There IS a selectedIndex property of the
"select" element.
subtotal=+(document.forms[0].d1.options.[ind].value);

Non-existent operator again. Now, you're assigning the value of an option in
the "options" array of the "select" element to a variable. However, the
value is I don't know what, because of what I told you previously.

The rest is like a set of dominoes which all fall down because the
foundation has crumbled.

I'll take a stab at correcting it, but it's so mucked up I'm likely to fill
in some gaps incorrectly:

<script language="javascript">
function calculatecost() {
var totalcost,discount,subtotal,ind;
discount = parseInt((document.forms[0].givendiscount.value);
ind = document.forms[0].d1.selectedIndex;
subtotal = parseInt((document.forms[0].d1.options.[ind].value));
totalcost = subtotal - (subtotal/discount};
document.forms[0].subtotalvalue.value = subtotal;
document.forms[0].discountvalue.value = discount;
document.forms[0].totalcostvalue.value = totalcost.toFixed(2);
}
</script>

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.


John said:
Hi all,
I would welcome any assistance with this problem.

I have an Input Textbox named 'givendiscount'
I have a drop down box named 'd1'

I have an Input Textbox named 'subtotalvalue'
I have an Input Textbox named 'discountvalue'
I have an Input Textbox named 'totalcostvalue'

What should happen is that a user enters a discount amount in
'givendiscount'
He/She then chooses an option from 'd1'

After clicking a button (the code says onClick="calculatecost()") then
three values should be
entered into the boxes.

Here is the function...

<script language="javascript">
function calculatecost() {
var totalcost,discount,subtotal,ind;
discount=+(document.forms[0].givendiscount.value);
ind=+(document.forms[0].d1.options.selectedIndex);
subtotal=+(document.forms[0].d1.options.[ind].value);
totalcost=subtotal-(subtotal/discount};
document.forms[0].subtotalvalue.value=subtotal;
document.forms[0].discountvalue.value=discount;
document.forms[0].totalcostvalue.value=totalcost.toFixed(2);
}
</script>

The code is in the HEAD section of the page.

Trouble is, nothing is happening at all...not even any error codes.
Can anyone help please.

TIA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

textbox Disable on value.... 2
Function... 15
HELP: Why won't this javascript work? 1
Dropdown events... 2
checkbox doesn´t work 5
Javascript question... 1
Script Question... 4
DRW Submit Button... 5

Top