user2338015
user2338015

Reputation: 11

Search within HTML code?

How do I search within HTML code with JQuery? I want to find a string in the HTML code and then hide the div. This does not work:

<div class="example">
test
<img src="example.jpg"></div>
$(document).ready(function(){
    $(".example:contains('example.jpg')").hide();
});

But when I tell it to search for 'test' instead of 'example.jpg', it does work?

Upvotes: 1

Views: 112

Answers (3)

Kevin B
Kevin B

Reputation: 95031

Combine the :has and the attribute-equals selectors.

$('.example:has([src="example.jpg"])').hide();

http://jsfiddle.net/J9bgt/

this would be more efficient, due to the fact that :has is a custom selector implemented by jQuery:

$('.example').filter(':has([src="example.jpg"])').hide();

Upvotes: 2

Ankush Jain
Ankush Jain

Reputation: 1527

try the following

$(document).ready(function(){
if($('.example').html().indexOf('example.jpg')!=-1){
$('.example').hide();
}
});

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

$('img[src="example.jpg"]').parent().hide();

But this is very tightly coupled code, you should really design your markup better in order to deal with it.

Upvotes: 1

Related Questions