简体   繁体   中英

Vanilla JS hide Div if contains a string

I'm dealing with a CMS that sometimes returns error codes when displaying products.

I'm currently learning basic JS, searched all over stack overflow, and couldn't find an answer applicable.

On this page

In the products used section, there is an error code for "#ResourceNotFound: ProjectResources, inside# New Construction : No" that keeps appearing for every product.

Is there an easy way that javascript can be used to hide these snippets from the front end?

The codes I used to try was

document.querySelectorAll('.text-medium').forEach((element, index) => {
   if (element.innerText.includes('New Construction :  No')) {
       element.style.display = 'none';
   }
})

You need to check for the presence of either of those phrases, as they are in separate <div> elements. Also, use textContent instead of innerText .

document.querySelectorAll('.text-medium').forEach((element, index) => {
   if (element.textContent.includes('New Construction :  No')
    || element.textContent.includes('#ResourceNotFound: ProjectResources')) {
       element.style.display = 'none';
   }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM