Hi have a working JS function to add a div based on text inside an element, se below. In the example for something like
<div class="class">sometext</div>
This works well, but how do I make it for this exact match only, so it wont add the extra div if the element has another word (Which I know what it is)
Example where I dont want it to add the div:
<div class="class">sometext remove</div>
const descriptions = document.querySelectorAll(".class");
function showDescription() {
for (const description of descriptions) {
if (description.textContent.includes("sometext")) {
description.insertAdjacentHTML("beforeend", "<div></div>");
}
}
}
showDescription();
2
Answers
ou can use Regex for that :
here the ^ refers to the start of the string and $ refers to the end of it
containsSometext will return true if the est is true and false if not
You can simple use
===
like:If you have two words you can use
||
with===
or an array like:1:
2: