Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my function unable to return elements with specific text content?

I have a function, matchTagAndText which as the name says accepts 2 arguments a selector and text and checks if any of the matched elements has the given text. It goes like so:

function matchTagAndText(sel, txt) {
    var elements = document.querySelectorAll(sel);
    return Array.prototype.filter.call(elements, function(element){
        return RegExp(txt,'i').test(element.textContent);
    });
} 

Now I am trying to select a td element which contains text 'Lorem ipsum' in it but I am unable to do so.

My HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Frames</title>
</head>
<body>
    <td colspan="2" class="font-description">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempora nulla iste, quae necessitatibus. Impedit mollitia delectus eum voluptas ullam! Perspiciatis in dolorem blanditiis dolores mollitia sint nostrum sunt veniam est!
    </td>
</body>
</html>

I tried calling the function after defining it in console with:

var el = matchTagAndText('td.font-description','Lorem ipsum');

But this returns an empty array. What is wrong with my function?

Live Example:

function matchTagAndText(sel, txt) {
    var elements = document.querySelectorAll(sel);
    return Array.prototype.filter.call(elements, function(element){
        return RegExp(txt,'i').test(element.textContent);
    });
} 

var el = matchTagAndText('td.font-description','Lorem ipsum');
console.log(el);
<td colspan="2" class="font-description">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempora nulla iste, quae necessitatibus. Impedit mollitia delectus eum voluptas ullam! Perspiciatis in dolorem blanditiis dolores mollitia sint nostrum sunt veniam est!
</td>
like image 271
Nagarjun Prasad Avatar asked Mar 29 '18 07:03

Nagarjun Prasad


People also ask

What is the difference between innerText and textContent?

textContent gets the content of all elements, including <script> and <style> elements. In contrast, innerText only shows "human-readable" elements. textContent returns every element in the node. In contrast, innerText is aware of styling and won't return the text of "hidden" elements.

What is the textContent in JavaScript?

In JavaScript, the textContent property allows us to get or set the text content of a node, or an element. Setting the textContent property will remove and replace the child nodes with the new text node. The textContent property returns the content of an element/node and all of its descendents(child nodes).

What is the difference between innerHtml and textContent?

textContents is all text contained by an element and all its children that are for formatting purposes only. innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.

What is the property textContent?

The textContent property in HTML is used to set or return the text content of the specified node and all its descendants. This property is very similar to nodeValue property but this property returns the text of all child nodes. Syntax: It is used to set the text of node.


2 Answers

The problem, intriguingly, is that your markup is invalid: You have td with no surrounding tr or table. If you fix that, it works:

function matchTagAndText(sel, txt) {
    var elements = document.querySelectorAll(sel);
    return Array.prototype.filter.call(elements, function(element){
        return RegExp(txt,'i').test(element.textContent);
    });
} 

var el = matchTagAndText('td.font-description','Lorem ipsum');
console.log(el);
<table>
  <tr>
    <td colspan="2" class="font-description">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempora nulla iste, quae necessitatibus. Impedit mollitia delectus eum voluptas ullam! Perspiciatis in dolorem blanditiis dolores mollitia sint nostrum sunt veniam est!
    </td>
  </tr>
</table>

If we inspect your example using Chrome's devtools, we can see that in its attempt to make the HTML valid, it's just completely stripped your td tag, and its associated class, so your selector doesn't match anything:

enter image description here

like image 125
T.J. Crowder Avatar answered Oct 09 '22 09:10

T.J. Crowder


Your code is fine, your markup is not - A TD should live inside a row inside a table.

function matchTagAndText(sel, txt) {
  var elements = document.querySelectorAll(sel);
  return Array.prototype.filter.call(elements, function(element) {
    return RegExp(txt, 'i').test(element.textContent);
  });
}

var el = matchTagAndText('td.font-description','Lorem ipsum');

console.log(el);
<table>
  <tbody>
    <tr>
      <td colspan="2" class="font-description">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempora nulla iste, quae necessitatibus. Impedit mollitia delectus eum voluptas ullam! Perspiciatis in dolorem blanditiis dolores mollitia sint nostrum sunt veniam est!
      </td>
    </tr>
  </tbody>
</table>
like image 3
Ori Drori Avatar answered Oct 09 '22 11:10

Ori Drori