A function called matchTagAndText is designed to take two arguments: a selector and text, checking if any matched elements contain the specified text. The function code is as follows:
function matchTagAndText(sel, txt) {
var elements = document.querySelectorAll(sel);
return Array.prototype.filter.call(elements, function(element){
return RegExp(txt,'i').test(element.textContent);
});
}
The task at hand is to select a td
element that includes the text 'Lorem ipsum', but this seems to be proving challenging.
The HTML content provided for this challenge:
<!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>
Following unsuccessful attempts to call the function with:
var el = matchTagAndText('td.font-description','Lorem ipsum');
An empty array gets returned. It seems there might be an issue with the function implementation. What could be causing this problem?
For a live example, see below:
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>