tags:

views:

31

answers:

2

I'm trying to figure out how I'd add a contains to check that the responseText coming back in the callback of a jQuery .load() contains a certain string of text:

   $("#ordersList").load("OrderHandler.ashx?action=" + action, function(responseText, textStatus, XMLHttpRequest)
   {
                   if(responseText)
                   {
                    // do something
                           return;
                   }

   });

so if responseText contains a certain string anywhere in the response that was sent back from the server I can perform appropriate processing in my callback function here.

+4  A: 
    if(responseText.indexOf('mySearchString') > -1) {
         // do something
         return;
    }
patrick dw
weird, jQuery Intellisense in Visual Studio 2010 does not show .indexOf...but looks like it can be applied on any jQuery element?
CoffeeAddict
@coffeeAddict - It can be applied to a string. Not to a jQuery object.
patrick dw
Thanks Patrick for pointing out the obvious ;) I think I'm just lacking sleep today and was frustrated when it was right in front of my face.
CoffeeAddict
@coffeeaddict - Been there many times. :o)
patrick dw
+1  A: 

The responseText is literally text. You dont need Jquery for this if you just want to look for an instance anywhere in the string.

if(responseText.indexOf("the string im lookingfor") > -1)
{
   // do stuff
}
John Hartsock
Thanks John. I was thinking it was a jQuery object..but it's just a string param! duh
CoffeeAddict
@John - This won't quite work, because the index returned may be 0, which will equate to `false`. You need to test that it is not less than 0, as `-1` is returned if there is no match.
patrick dw
the check doesn't work without the > -1
CoffeeAddict
yep sorry about that
John Hartsock