469,649 Members | 1,241 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

Greater than operator problem

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.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.valu e);
if (document.labels.elements.frmQuantityDelivered.val ue >
document.labels.elements.frmMaxQuantityPerBox.valu e)
{
alert('Stage 3');
document.labels.elements.frmLabelQty1.value ==
document.labels.elements.frmMaxQuantityPerBox.valu e;
}
else
{
alert('Stage 4');
alert(document.labels.elements.frmQuantityDelivere d.value /
document.labels.elements.frmMaxQuantityPerBox.valu e);
}
}

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

The problem occurs when "if
(document.labels.elements.frmQuantityDelivered.val ue >
document.labels.elements.frmMaxQuantityPerBox.valu e)" 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.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.valu e);" 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

Feb 6 '07 #1
3 1404
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.va lue >
document.labels.elements.frmMaxQuantityPerBox.val ue)" 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.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.val ue);" 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.va lue >
+document.labels.elements.frmMaxQuantityPerBox.val ue)

The unary + operator converts the strings to numbers.
--

Feb 6 '07 #2
Nice one, thanks heaps for that!

ab

Feb 6 '07 #3
In comp.lang.javascript message <11**********************@h3g2000cwc.goo
glegroups.com>, Tue, 6 Feb 2007 14:11:05, andybeh <el*****@hotmail.com>
posted:
>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.

<FAQENTRY4.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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Emanuel Ziegler | last post: by
3 posts views Thread by Alex Vinokur | last post: by
2 posts views Thread by John Mark Howell | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.