I have this html:
<td class="catName" data-bs-toggle="collapse" data-bs-target=".category1">A</td>
<td class="catName" data-bs-toggle="collapse" data-bs-target=".category2">B</td>
<td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">C</td>
I want to use the filter
function to find the class that has the innerText "A".
Trying with:
jQuery(".catName").filter(item => jQuery(item).text() == "A");
But it doesn’t work.
I am using Jquery
. I have to use filter
. And I have to use an arrow function
.
2
Answers
Due to your markup being invalid stands nothing will work. You need to add some corresponding table elements.
The first argument of
filter
isindex
, anditem
is the second argument (which is the opposite way native JS array methods work).In this example I’m filtering the cell with "A" and turning its background red.
At a minimum
<td>
elements need to be wrapped inside<table>
element tags for it to make sense to the DOM.The other thing you need to change is that the JQuery
filter()
methods receives theindex
first, and the JQueryitem
as the second argument. So you needfilter((i, item) =>
$(".catName").filter((i, item) => $(item).text() == "A");
See JSFiddle for full example:
https://jsfiddle.net/Lexdp7rt/