1

i have to get them via a selector and add a class.

<div class="count_1">
<span class="percentage">10 %</span>
</div>
<div class="count_2">
<span class="percentage">90 %</span>
</div>


if (parseInt($(".count_1.percentage").value) > $(".count_2.percentage").value)) {
            blabla
        }

3 Answers 3

5

This should do:

if (parseInt($(".count_1 .percentage").html(), 10) > parseInt($(".count_2 .percentage").html(), 10))

Note that the space in the selector denotes child elements. .count_1.percentage matches elements with class="count_1 percentage", wheareas .count_1 .percentage matches class="percentage" within class="count_1"

Also note the need to wrap both values in parseInt, and I also added a second parameter to the parseInt call which explicitly parses in base 10.

parseInt('08', 10); // 8
parseInt('08'); // 0
4
  • 2
    I'd use text() instead of html(), to strip out possible HTML tags. Probably doesn't matter in this case, though.
    – Geert
    Jan 4, 2010 at 13:51
  • Also, I believe plain class selectors are quite slow. I'd add the element name: $('div.count_1 > span').
    – Geert
    Jan 4, 2010 at 13:53
  • good point, but yeah, the entire concept still makes heaps of assumptions about how the html code will look already Jan 4, 2010 at 13:54
  • yeah, that's also a good point. again, depending on how many assumptions we can allow ourselves to make, i guess we might as well have done var percentage = $('.percentage'); if(parseInt(percentage.eq(0).text(), 10) > parseInt(percentage.eq(1).text(), 10)) =) i don't really know what would be the fastest out of those Jan 4, 2010 at 13:56
0

Something like this. It would be better to use IDs for the wraper divs though ...

var val1 = parseInt ( $( '.percentage:first', $( '.count_1' ) ).html (), 10 );
var val2 = parseInt ( $( '.percentage:first', $( '.count_2' ) ).html (), 10 );

if ( val1 > val2 ) {
    // do stuff
}
2
  • 1
    converting a string value to int by parseInt feels a bit more conventional and readable than * 1, tho, so I don't see why you'd want to change that from the original example. it's also rids you the need for the replace since parseInt('123abc') will yield 123 Jan 4, 2010 at 13:51
  • why have 2 calls to the jQuery object? $('.count_1 .percentage') is shorter and easier to understand
    – nickf
    Jan 4, 2010 at 13:52
0

it's working, but i have multiple votingresult on the page. i am calling the script everytime with a pollid as a selector. but it's working only for the first pollresult. any suggestions?

if (parseInt($(".pollid_1 .count1 .percentage").html(), 10) > parseInt($(".pollid_1 .count2 .percentage").html(), 10))
$(".pollid_1 .count1 span.percentage").addClass("selected");
else
$(".pollid_1 .count2 span.percentage").addClass("selected");

<div class="pollid_1">
<div class="count_1">
<span class="percentage">10 %</span>
</div>
<div class="count_2">
<span class="percentage">90 %</span>
</div>
</div>



if (parseInt($(".pollid_2 .count1 .percentage").html(), 10) > parseInt($(".pollid_2 .count2 .percentage").html(), 10))
$(".pollid_2 .count1 span.percentage").addClass("selected");
else
$(".pollid_2 .count2 span.percentage").addClass("selected");

<div class="pollid_2">
<div class="count_1">
<span class="percentage">10 %</span>
</div>
<div class="count_2">
<span class="percentage">90 %</span>
</div>
</div>

and so on...

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.