How to identify line end in Javascript / jQuery?

Asked

Viewed 257 times

-3

I have the following code:

var cont1 = $('.stat_col:contains("Total de páginas impressas")').next();
var cont2 = $('.stat_col:contains("Total de páginas impressas em preto e branco")').next();
var cont3 = $('.stat_col:contains("Total de páginas coloridas impressas")').next();
var cont4 = $('.stat_col:contains("Total de páginas impressas de um só lado")').next();
var cont5 = $('.stat_col:contains("Total de páginas impressas de ambos os lados")').next();
var cont6 = $('.stat_col:contains("Total de atolamentos")').next();
var cont7 = $('.stat_col:contains("Total de separações perdidas")').next();
var cont8 = $('.stat_col:contains("Total de páginas digitalizadas do ADF")').next();
var cont9 = $('.stat_col:contains("Total de páginas digitalizadas de ambos os lados")').next();
var cont10 = $('.stat_col:contains("Total de páginas do vidro do scanner")').next();
var cont11 = $('.stat_col:contains("Total de atolamentos")').next();
var cont12 = $('.stat_col:contains("Total de separações perdidas")').next();
var cont13 = $('.stat_col:contains("Total de cópias")').next();
var cont14 = $('.stat_col:contains("Total de cópias em preto e branco")').next();
$(cont1).append($(" - " + cont1).html());

For example the HTML content of the line that cont1 search is:

<tr class="gui-list-tbl-even-row"><td class="stat_col">Total de páginas impressas</td><td class="addrTemp_col">5382</td></tr>

The problem that for example everything you have in 'cont1', also has in 'cont2', 4 and 5.

In addition to capturing each 'counter', I need to be able to inject (dynamic) information in each of them based on the corresponding value of each line.

1 answer

0

Maybe you don’t even need regex. Just search for elements whose class is stat_col, and use filter to filter these elements by exact text:

let cont1 = $('.stat_col').filter(function() {
    return $(this).text() === "Total de páginas impressas";
}).next();
let cont2 = $('.stat_col').filter(function() {
    return $(this).text() === "Total de páginas impressas em preto e branco";
}).next();
console.log(cont1.text()); // 5382
console.log(cont2.text()); // 123
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="gui-list-tbl-even-row"><td class="stat_col">Total de páginas impressas em preto e branco</td><td class="addrTemp_col">123</td></tr>
<tr class="gui-list-tbl-even-row"><td class="stat_col">Total de páginas impressas</td><td class="addrTemp_col">5382</td></tr>
</table>

When making the comparison with ===, I am searching for the exact text. If you are always searching for fixed texts, using regex is an exaggeration.


It is also possible to do the same searches without jQuery:

function filtrar(texto) {
    return Array.prototype.filter.call(
        document.querySelectorAll('.stat_col'),
        function(node) {
            return node.innerText === texto;
        }
    );
}

let cont1 = filtrar("Total de páginas impressas")[0].nextSibling;
let cont2 = filtrar("Total de páginas impressas em preto e branco")[0].nextSibling;
console.log(cont1.innerText); // 5382
console.log(cont2.innerText); // 123
<table>
<tr class="gui-list-tbl-even-row"><td class="stat_col">Total de páginas impressas em preto e branco</td><td class="addrTemp_col">123</td></tr>
<tr class="gui-list-tbl-even-row"><td class="stat_col">Total de páginas impressas</td><td class="addrTemp_col">5382</td></tr>
</table>

In the case, filter returns an array (not the same filter jQuery), then I’m only taking the first element ([0]), but if there can be more than one, just make a for in this array and treat the elements the way you need them.

  • hkotsubo in both jQuery and no jQuery cases the result on the console comes Ok, but in the already error append because the variable is an object (Uncaught Error: Syntax error, unrecognized Expression: - [Object Htmltablecellelement]), and I will need to insert information on each line.

  • @Danielplácido In the above examples I get the text from each cell (using text() jQuery or innerText in pure JS. If that’s not what you need, just edit the question and add more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.