0

I have a page with a a lot of texts and a script called "test.php" elsewhere.

I want to look up a word, lets say "oranges" in the div class "paragraph", and if there is "oranges" word in there, i want it to load test.php

I looked at the examples for find() function but i failed to make it work as i wanted it to.

Thanks.

Tumay
  • 488
  • 2
  • 6
  • 16
  • What is your question? Can you show what you have tried? – Pekka Aug 15 '11 at 07:47
  • possible duplicate of [Find text string using JQuery?](http://stackoverflow.com/questions/926580/find-text-string-using-jquery) – Aaron Digulla Aug 15 '11 at 07:49
  • I think you need to clarify what you mean by "load test.php" - you have 3 different interpretations here, which one is right? – GregL Aug 15 '11 at 07:52

3 Answers3

1
if($(".paragraph").text().indexOf("oranges") > -1) {
    $.get("script.php", function() {
       //Loaded!
    });  
}

This gets the text of .paragraph and checks whether that string contains the text "oranges" using the native JavaScript indexOf method. If the string does contain the specified text, you can load your script.

Note that this will be case sensitive, so if you also wanted to match "Oranges", you could use the toLowerCase method before indexOf, or use a regular expression.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
0
if($(".paragraph:contains('Oranges')").length > 0) {
    location.href = 'test.php';
}

The find() function is used to find descendants of the jQuery object whose find() you are calling.

MeLight
  • 5,454
  • 4
  • 43
  • 67
0

Load test.php and put it's contents in the div with class paragraph if that div contains the text "oranges":

$('div.paragraph:contains("oranges")').load('test.php');
Paul
  • 139,544
  • 27
  • 275
  • 264