-1

any if statement I put inside this function refuses to work.
The statement is true for some elements with this class and false for some, however all ignore it
(This is very stripped functionality just to locate the problem...)

$(".bought").each(function(index) {
Height = $(this).css("height")
    if (Height > 19) {
        alert(Height);
    }
});

this does work:

$(".bought").each(function(index) {
Height = $(this).css("height")
    alert(Height);
});
2
  • 1
    Try debugging to see Height value and if you ever get to the if statement.
    – Giann
    Jun 28, 2011 at 10:49
  • There appears to be a missing ; on line two in the first block, is this the problem?
    – aligray
    Jun 28, 2011 at 10:51

4 Answers 4

4
$(".bought").each(function(index) {
    thisHeight = $(this).height()
    if (thisHeight > 19) {
        alert(thisHeight);
    }
});

That works! http://jsfiddle.net/NmHb3/

2

$(this).css("height") returns string...you need to parse this in int before applying comparison..

 Height = parseInt($(this).css("height"),10)
if (Height > 19) {         alert(Height);     }
1
  • parseInt($(this).css("height"), 10) Jun 28, 2011 at 10:59
1

Height is returned as a string. The comparision is actually "19" > 19 which yields false.

Try converting it to an integer:

if (parseInt(Height) > 19) {
// ...
}
1
  • But "20" > 19 yields true. The string is converted to a number. And never forget to pass the radix to parseInt! Jun 28, 2011 at 10:58
0

The below code which you have used returns 'px' appended to the result so you can't compare it to '19'

Height = $(this).css("height")

Parse it like this and let us know how you get on :)

parseInt($(this).css('height'), 10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.