Reputation: 1015
i have a question that if i want to search a perticular text in any page using jquery. what i don't want is that
<p id="1">iii</p>
<p id="2">i</p>
<p id="3">ibiib</p>
<p id="4">bb</p>
<p id="5">tina</p>
<script type="text/javascript">
console.log($("div:contains('i')"));
</script>
this above code gives me all paragraphs except id=4; but what i want is only paragraph with id=2 to be selected. if there is a way to do that in javascript or any other library reply me...
Upvotes: 0
Views: 2843
Reputation: 4035
You can extend jQuery's selector engine to add a new one:
$.expr[':'].texteq = function( elem, i, match, array ) {
return $(elem).text() === match[3];
};
console.log($('p:texteq(i)'));
Upvotes: 1
Reputation: 342635
I would use a simple plugin for this:
$.fn.exactMatch = function(str) {
return this.filter(function() {
return $(this).text() === str;
});
};
$("p").exactMatch("i").css("border", "1px solid red");
Upvotes: 1
Reputation:
You can loop through the paragraphs and check if their content is the same as your desired content. I used a jQuery loop, you can use javascript if you want. JSFIDDLE
$('p').each(function() {
if(this.innerHTML == 'i') doWhateverYouWant();
});
Upvotes: 1
Reputation: 50976
$("p").each(function(a,b){
if ($(this).html() == 'i'){
console.log(a);
}
});
or
$("p").filter(function(){
return $(this).html() == 'i';
});
Upvotes: 1
Reputation: 219930
$('p').filter(function() {
return $(this).text() === 'i';
});
Upvotes: 4
Reputation: 37633
Use $.filter.
This will return the div
s, which contains just one i
.
$('div').filter(function () {
return $(this).html() == 'i';
});
Upvotes: 5