Reputation: 11
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
Reputation: 95031
Combine the :has and the attribute-equals selectors.
$('.example:has([src="example.jpg"])').hide();
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
Reputation: 1527
try the following
$(document).ready(function(){
if($('.example').html().indexOf('example.jpg')!=-1){
$('.example').hide();
}
});
Upvotes: 0
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