PSoft
PSoft

Reputation: 95

Search text in html by javascript

I have some html like this:

<html>
  <body>
    <div>
      <p>Something</p>
    </div>
    <div class="hide" id="show">Protected</div>
  </body>
</html>

I need to display or hide/show an element via JavaScipt if html has "Something" in its text. How can I do that? I need this for my Wordpress page.

Upvotes: 6

Views: 17624

Answers (1)

Bian Goole
Bian Goole

Reputation: 205

Without using jQuery:

var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf("Something")!==-1;
if (hasText) {
    document.getElementById("show").style.display = 'block';
} else {
    document.getElementById("show").style.display = 'none';
}

Upvotes: 9

Related Questions