- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Hide div that contains specific text with JavaScript?
At first, you need to extract the extract the div class with the help of getElementsByClassName() and iterate over the for loop and use the OR condition to show the specific text.
Also, set yourDiv.style.display=’none’.
Following is the JavaScript code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <div class="block"> header </div> <div class="block"> content </div> <div class="block"> footer </div> <script> let attribute = document.getElementsByClassName('block'); for (let i = 0; i < attribute.length; i++) { let impDiv = attribute[i]; let value = impDiv.innerHTML.trim(); if (value == 'header' || value == 'footer') { impDiv.style.display = 'none'; } } </script> </body> </html>
To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option open with live server in VS code editor.
Output
- Related Articles
- Toggle hide class only on selected div with JavaScript?
- Replace HTML div into text element with JavaScript?
- How do I find an element that contains specific text in Selenium Webdriver?
- How to hide a div in JavaScript on button click?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?
- Find document with array that contains a specific value in MongoDB
- Find MongoDB documents that contains specific field?
- How to convert comma separated text in div into separate lines with JavaScript?
- Find documents that contains specific field in MongoDB?
- Get MongoDB documents that contains specific attributes in array
- Find a strict document that contains only a specific field with a fixed length?
- How to get content of div which contains JavaScript script blocks?
- Select rows that contain specific text using Pandas
- How to hide HTML element with JavaScript?
- How to use JavaScript to hide a DIV when the user clicks outside of it?

Advertisements