Keith Donegan Keith Donegan - 1 year ago 126
jQuery Question

Find text string using jQuery?

Say a web page has a string such as "I am a simple string" that I want to find. How would I go about this using JQuery?

Answer Source

jQuery has the contains method. Here's a snippet for you:

<script type="text/javascript">
$(function() {
    var foundin = $('*:contains("I am a simple string")');

The selector above selects any element that contains the target string. The foundin will be a jQuery object that contains any matched element. See the API information at:

One thing to note with the '*' wildcard is that you'll get all elements, including your html an body elements, which you probably don't want. That's why most of the examples at jQuery and other places use $('div:contains("I am a simple string")')

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download