Reputation: 664
What's the easiest way to search a div for a text string using jquery? I specifically want to search a div with an id and see if it contains $(this).text()
. The purpose of this is to be able to append elements to a div but be able to prevent the same string from being written to the same div over and over.
Upvotes: 1
Views: 127
Reputation: 1075427
I specifically want to search a div with an id and see if it contains
$(this).text()
.
I think I'd probably go with String#indexOf
:
var div = $("#theId"),
text = $(this).text();
if (div.text().indexOf(text) === -1) {
// It doesn't have it, add it
div.append(text); // Or whatever
}
Upvotes: 2