tags:

views:

648

answers:

3

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
        }
+4  A: 

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
David Hedlund
I'd use `text()` instead of `html()`, to strip out possible HTML tags. Probably doesn't matter in this case, though.
Geert
Also, I believe plain class selectors are quite slow. I'd add the element name: `$('div.count_1 > span')`.
Geert
good point, but yeah, the entire concept still makes heaps of assumptions about how the html code will look already
David Hedlund
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
David Hedlund
A: 

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
}
Jan Hančič
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`
David Hedlund
why have 2 calls to the jQuery object? `$('.count_1 .percentage')` is shorter and easier to understand
nickf
@David: point there! @nickf: a matter of preference if you ask me ...
Jan Hančič
A: 

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

Alexander