I have a table like this:
<table id="comparison">
<tr>
<th>Hotels</th>
<th>Jan 2013</th>
<th>Feb 2013</th>
</tr>
<tr>
<td>Lorem Ipsum Hotel</td>
<td>
<div class="rooms">165</div>
<div class="sob">352</div>
</td>
<td>
<div class="rooms">215</div>
<div class="sob">30</div>
</td>
</tr>
</table>
I prepared this jQuery script to compare the Rooms and SOB in each cell.
- If the SOB is less than the Rooms then I color the SOB red to signal a negative result.
- If the SOB is greater than the Rooms then I color SOB green to signal a positive result.
$('#comparison td .sob').each(function() {
var rooms = $(this).prev().text();
var sob = $(this).text();
if (rooms > sob) {
$(this).css({'color': 'red'});
} else {
$(this).css({'color': 'green'});
}
});
The problem is that this script is not comparing the Rooms and SOB correctly. It seems to be running at random in the variable comparison, sometimes producing the correct result and other times not.
What am I missing?