This query selector doesn’t work on this HTML. It works in CSS. Can anyone tell me the correct solution?
Basically, I am using an older version of the material table and want to hide the “Export as PDF” option. I know the newer version allows it in exportButton option.
JavaScript
x
37
37
1
<div
2
class="MuiPaper-root MuiMenu-paper MuiPopover-paper MuiPaper-elevation8 MuiPaper-rounded"
3
tabindex="-1"
4
style="
5
opacity: 1;
6
transform: none;
7
transition: opacity 215ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,
8
transform 143ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
9
top: 181px;
10
left: 1754px;
11
transform-origin: 0px 19.5px;
12
"
13
>
14
<ul
15
class="MuiList-root MuiMenu-list MuiList-padding"
16
role="menu"
17
tabindex="-1"
18
>
19
<li
20
class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
21
tabindex="0"
22
role="menuitem"
23
aria-disabled="false"
24
>
25
Export as CSV<span class="MuiTouchRipple-root"></span>
26
</li>
27
<li
28
class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
29
tabindex="-1"
30
role="menuitem"
31
aria-disabled="false"
32
>
33
Export as PDF<span class="MuiTouchRipple-root"></span>
34
</li>
35
</ul>
36
</div>
37
JavaScript
1
2
1
document.querySelectorAll("ul.MuiMenu-list li:contains('Export as PDF')")[0].remove();
2
Advertisement
Answer
You need JQuery to use the :contains()
Selector
JavaScript
1
2
1
$('td:contains("male")')
2
Also there is no CSS selector targeting on textContent. Take a look at full list of CSS3 Selectors
We need another method here :
JavaScript
1
10
10
1
function querySelectorIncludesText(selector, text) {
2
return Array.from(document.querySelectorAll(selector)).find((el) =>
3
el.textContent.includes(text)
4
);
5
}
6
7
const find = querySelectorIncludesText("li", "Export as PDF");
8
9
console.log(find);
10
Now you have the correct element.
To hide an object you should NOT remove that ! You can simply change display
property of element :
JavaScript
1
2
1
find.style.display = "none";
2