Let's separate in steps what should be done:
Get all values within each td
.
Places them in order from lowest to highest.
Get the element with the lowest value and insert the tag <mark></mark>
into it.
Step 1:
Gathering all elements:
$('#precos td.pointer');
Step 2:
Place the elements in order according to the value inside the HTML
, for this we can use the sort()
function that takes a loop by taking two and two elements and comparing them:
var values = $('#precos td.pointer').sort(function(current, next) {
// Substitui a string R$ por "vazio" e pega apenas os
// valore dentro do HTML e transforma em números.
var one = parseFloat(current.innerHTML.replace('R$', ''));
var two = parseFloat(next.innerHTML.replace('R$', ''));
// Verifica se o valor atual é maior ou menor que o próximo
if (one < two) return -1;
return 1;
});
Now the variable values
has all elements in descending order (lowest to highest), so we can simply get the first element of values
which will always be the largest and change the HTML
$(values[0]).html('<mark>'+values[0].innerHTML+'</mark>');
See below the working code:
var values = $('#precos td.pointer').sort(function(current, next) {
// Pega apenas os valore dentro do HTML e transforma em números.
var one = parseFloat(current.innerHTML.replace('R$', ''));
var two = parseFloat(next.innerHTML.replace('R$', ''));
// Verifica se o valor atual é maior ou menor que o próximo
if (one < two) return -1;
return 1;
});
$(values[0]).html('<mark>'+values[0].innerHTML+'</mark>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tablewidth="300px" border="1px" bordercolor="#FF0000">
<tbody id="qtdPrecos">
<tr id="precos">
<td class="pointer fmp-td">R$ 92.4</td>
<td class="pointer fmp-td">R$ 137.48</td>
<td class="pointer fmp-td">R$ 108.27</td>
<td class="pointer fmp-td">R$ 129.25</td>
</tr>
</tbody>
</table>