Satish
Satish

Reputation: 6485

Jquery: How to i find and elements like this <p>MyHeader</p>

Using Jquery, how do I find and hide elements like this?

<p>MyHeader</p>

The only identifier is MyHeader here, so trying to find elements that exactly match this and hide them.

Edit: I don't have a choice to add ids or class selectors, that would have made life easier :-) anyway, I found jquery contains seems to be of help!

Upvotes: 3

Views: 90

Answers (5)

gabitzish
gabitzish

Reputation: 9691

$("p:contains('MyHeader')").hide()

Upvotes: 1

Gaz Winter
Gaz Winter

Reputation: 2989

One way to do this is to add a class to the p tag.

So you would do something like this:

 <p class="myClass">MyHeader</p>

Then you can hide that using the following jQuery

 $(".myClass").hide()

Upvotes: 1

Darcey
Darcey

Reputation: 1987

Add id or class definition to the p tag

HTML

<p class="header">MyHeader</p>

JQUERY

$('.header').hide();

Upvotes: 0

Igal Zeifman
Igal Zeifman

Reputation: 1146

Jquery API has a Contain function:

http://api.jquery.com/category/selectors/content-filter-selector/

This is an example:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div>John Resig</div>

<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>


<script>
$("div:contains('John')").css("text-decoration", "underline");
    </script>

</body>
</html>

Upvotes: 1

elclanrs
elclanrs

Reputation: 94121

$('p:contains(MyHeader)').hide()

Upvotes: 6

Related Questions