views:

229

answers:

2

Hello guys,

I'm having problem with the contains in jquery. It seems that it only accepts one word. Not a phrase or two words.

Example:

$('#div:contains('Word')'); --> This is okay

$('#div:contains('Just another word')'); --> This will return empty/this will not match.

Have you experience this kind of problem?

Your reply is greatly appreciated.

Best regards,
Belmer

+3  A: 

What you need, is to use double quotes (instead those single quotes wrapping the whole selector), for example:

$("p:contains('John Resig')");

this will select the correct paragraph, with string 'John Resig' inside

or you can inverse it:

$('p:contains("John Resig")');

or you can use an old good escaping:

$('p:contains(\'John Resig\')');
Juraj Blahunka
+1  A: 

Try it without the # and avoid using the same quoting within the code:-

$("div:contains('Just another word')");

Brian O'Connell
Oh yeah, didn't notice that samer was using a '#' in front of the selector. That and the quotes must be why its not working :-P
Pandincus
I'm sorry for not being so clear. This is the right thing, $('#div') --> $('#divid');
samer
Not sure what you mean samer. You only use the # when you want to get a specific element by id. I'm pretty sure contains doesn't work in those cases as it's intended to find any of the specified elements containing the text you include. To check the contents of a specific element would be more along the lines of if ($("#divid").html().indexOf("my string")>-1)
Brian O'Connell