You can use https://www.w3schools.com/jsref/met_document_queryselector.asp. If you know how to write a CSS selector for jQuery, that's how you can use it without jQuery.
$('div > ul > li')
becomes document.querySelectorAll("div > ul > li");
Of course:
- I haven't selected the
div
, but the li
.
- I didn't check that the piece of text was included in the
li
.
So how do we do that? We can't do it with CSS selectors, so we're going to use plain JS. We'll filter only the li
elements that contain the text we want, then we'll take their grand-parent node (which is the div
).
// Get the li's in an array
let lis = Array.from(document.querySelectorAll("div > ul > li"));
// only keep the ones with the proper text
lis = lis.filter(item => item.text.match(/The text that you want to match/));
// get the div's from the li's
let divs = lis.map(item => item.parentNode.parentNode);
// do whatever you want to these divs.
...
This approach is more resilient than calling .innerHTML
directly on the div, because in that scenario you might end up selecting div's with the required text somewhere random in their innerHTML. For instance something like:
<div class="uc-events">
<h3 style="text-align: center;">Upcoming Events</h3>
<ul class="upcoming_events graphical_theme">
<li>No upcoming events are scheduled </li>
</ul>
</div>
<div class="uc-events">
<h3 style="text-align: center;">Upcoming Events</h3>
<ul class="upcoming_events graphical_theme">
<!--<li>No upcoming events are scheduled </li>-->
<li>Event 1: 11/10 at 08:00</li>
</ul>
</div>
<div class="uc-events">
<h3 style="text-align: center;">Upcoming Events</h3>
<ul class="upcoming_events graphical_theme">
<li>Event 2: 12/10 at 08:00</li>
</ul>
<span>After that, you'll find out that No upcoming events are scheduled.</span>
</div>
Here with the .innerHTML
methods from the other answers, all 3 div's are going to be hidden, instead of just the top one.