Let’s separate by steps what must be done:
- Take all values within each
td
.
- Put them in order from minor to major.
- Take the lowest value element and insert the tag
<mark></mark>
in it.
Step 1:
Taking all the elements:
$('#precos td.pointer');
Step 2:
Set elements in order according to the value within the HTML
, for this we can use the function sort()
that performs a loop taking 2 in 2 elements and comparing between 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 (smaller to larger), and so we can simply take the first element of values
that will always be the greatest and change the HTML
$(values[0]).html('<mark>'+values[0].innerHTML+'</mark>');
See below the code working:
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>
<table width="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>
First, identify the cell with the lowest value. Have you already achieved this? If yes, post the code. If not, describe how you think it’s done.
– Woss
I couldn’t. I thought I’d list all the
<td>
who are inside the<tr>
with the id#precos
using the$.each
jQuery then take the one that has the lowest value using theMath.min.apply(Math, variavel)
. Then add the tag and replace the<td>
. But I couldn’t– usuario
The idea sounds right. Can you still post this code you tried to do? It will help you identify exactly what your mistake was.
– Woss
I could only find the lowest value: https://jsfiddle.net/newtech/gr9xn8as/
– usuario
Right. Now you go through all the elements
td
, checks which has the value equal to the minimum and adds the tagmark
.– Woss
Question: and if there are two equal prices, both should be marked or what would be the selection criteria?
– Woss
There will never be two equal prices. I group the same values before listing in the table
– usuario
You could edit the title of the question @user, you’re confused.
– BrTkCa