Greater than operator problem

A

andybeh

Hi All,

Can anyone tell me why the following code does not evaluate correctly
on my web page:

function calcLabels()
{

alert('Stage 1');
if (document.labels.elements.frmBundlesPerBox.value > 0)
{
alert('Stage 2');
alert('frmQuantityDelivered = ' +
document.labels.elements.frmQuantityDelivered.value + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.value);
if (document.labels.elements.frmQuantityDelivered.value >
document.labels.elements.frmMaxQuantityPerBox.value)
{
alert('Stage 3');
document.labels.elements.frmLabelQty1.value ==
document.labels.elements.frmMaxQuantityPerBox.value;
}
else
{
alert('Stage 4');
alert(document.labels.elements.frmQuantityDelivered.value /
document.labels.elements.frmMaxQuantityPerBox.value);
}
}

The values input are:
document.labels.elements.frmBundlesPerBox.value = 10
document.labels.elements.frmQuantityDelivered.value = 1000
document.labels.elements.frmMaxQuantityPerBox.value = 50

The problem occurs when "if
(document.labels.elements.frmQuantityDelivered.value >
document.labels.elements.frmMaxQuantityPerBox.value)" is evaluated.
The equation is supposedly calculating 1000>50, however it always
evaluates to false and displays the "Stage 4" alert. I know that it
is using the values of 1000 and 50 as "alert('frmQuantityDelivered = '
+ document.labels.elements.frmQuantityDelivered.value + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.value);" displays these
values.

I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.

Cheers

ab
 
L

Lee

andybeh said:
Hi All,

Can anyone tell me why the following code does not evaluate correctly
on my web page:
The problem occurs when "if
(document.labels.elements.frmQuantityDelivered.value >
document.labels.elements.frmMaxQuantityPerBox.value)" is evaluated.
The equation is supposedly calculating 1000>50, however it always
evaluates to false and displays the "Stage 4" alert. I know that it
is using the values of 1000 and 50 as "alert('frmQuantityDelivered = '
+ document.labels.elements.frmQuantityDelivered.value + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.value);" displays these
values.

I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.


The obvious thing you're missing is that form control values are
always strings, and the string "50" is greater than the string "1000".

if (+document.labels.elements.frmQuantityDelivered.value >
+document.labels.elements.frmMaxQuantityPerBox.value)

The unary + operator converts the strings to numbers.


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Can anyone tell me why the following code does not evaluate correctly
on my web page:

Many could.
I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.

You're probably really missing the FAQ, which is germane but not
explicit on the topic.

<FAQENTRY> 4.21 "Convert these to numbers before performing addition." -
add "or comparison".

It's a good idea to read the newsgroup and its FAQ. See below.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top