0

I have a table which I'm trying to extract the number of occurrences of a text match. The issue has sometimes the text is not visible in the table due to class="hide" so the queries I have tried are only returning the visible matches.

Example of what I have been trying is

Array.from(document.querySelectorAll('td:nth-child(2)'))
  .find(el => el.textContent.trim() == 'AU2014280123A')

I have also tried innerHtml and other variations.

Below is the elements which show there are 2 matches but the query above is only returning one

enter image description here

Any ideas on how I can return all matches regardless if they are hidden or not?

Thanks

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
user3803807
  • 285
  • 4
  • 18
  • It's not really clear what you're asking. Are you trying to _include_ or _exclude_ hidden elements? Your current code should return all elements, regardless of classnames – Phil Jul 20 '23 at 01:23
  • Sorry for confusion. I'm trying to include all hidden elements. Ideally, I just want the count of matches. eg. 2 in this case so if there is another way to write the query then open to that as well – user3803807 Jul 20 '23 at 01:24
  • They are already included ~ https://jsfiddle.net/8mw5p9j7/ – Phil Jul 20 '23 at 01:27
  • 2
    Your code uses [Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) so why is it surprising that it only returns one item? Use [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) if you want all the matches – Phil Jul 20 '23 at 01:28
  • When I run in console I only see one match – user3803807 Jul 20 '23 at 01:30
  • 2
    No kidding. That's what `find()` does... _"returns the first element in the provided array that satisfies the provided testing function"_ – Phil Jul 20 '23 at 01:31
  • ok, thanks. how do I return the number of matches then? – user3803807 Jul 20 '23 at 01:32
  • 1
    Does this answer your question? [How to count certain elements in array?](https://stackoverflow.com/q/6120931/283366) – Phil Jul 20 '23 at 01:33
  • yep, thanks. all good now. filter working – user3803807 Jul 20 '23 at 01:35

0 Answers0