1

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.

Do you have any experience with this kind of problem?

Your reply is greatly appreciated.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
mr.b
  • 2,708
  • 8
  • 39
  • 64
  • 1
    Shouldn't you be escaping those quotes? – Aistina Oct 16 '09 at 23:23
  • @Aistina: I think you're right -- :contains doesn't have this problem for me. Dunno why the first example would work, though. – Pandincus Oct 16 '09 at 23:29
  • Yes! I've tried that on already but I still got no result. – mr.b Oct 16 '09 at 23:30
  • Thanks for the reply guys. I'll post a code snippet. – mr.b Oct 16 '09 at 23:31
  • var selected = $(this).find(':selected').text(); if (selected.toLowerCase() != "all industries") { if (trim(selected).length > 0) { $("#contact-list-selectable li .contact-info-industry").parent().css("display", "none"); $("#contact-list-selectable li .contact-info-industry:contains('" + selected + "')").parent().css("display", "block"); alert($("li[style='display: block;']").text()); } – mr.b Oct 16 '09 at 23:33

3 Answers3

4

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
  • 17,913
  • 6
  • 34
  • 52
1

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

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

  • 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 Oct 16 '09 at 23:35
  • I'm sorry for not being so clear. This is the right thing, $('#div') --> $('#divid'); – mr.b Oct 16 '09 at 23:42
  • 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 Oct 17 '09 at 03:42
0

Assuming you didn't already solve this, from your comment which included:

var selected = $(this).find(':selected').text(); 
if (selected.toLowerCase() != "all industries") { 
    if (trim(selected).length > 0) { 
        $("#contact-list-selectable li .contact-info-industry").parent().css("display", "none"); 
        $("#contact-list-selectable li .contact-info-industry:contains('" + selected + "')").parent().css("display", "block"); 
        alert($("li[style='display: block;']").text()); 
}

I'll assume you meant to use trim as a method on the string, or if not that you have a trim function defined somewhere else. If this is actually how your code looks and you have no custom trim function, then that's going to be your first problem, it should be: selected.trim().length

One potential issue is that while you check that the trim of selected is > 0, you don't use a trimmed version of selected when checking the contains. If you have any spaces/etc in your selected variable, it will fail for contains.

Here is an example. Note the trailing space in selected = "computer science "; that is intentional to demonstrate what happens in that case.

if you change

 $("#contact-list-selectable li .contact-info-industry:contains('" + selected + "')").parent().css("display", "block");

to

 $("#contact-list-selectable li .contact-info-industry:contains('" + selected.trim() + "')").parent().css("display", "block");

you can avoid this issue (working example, see here, note the trailing space is still there).

The only other issue I could think of would be if you were incorrectly forming any of your selectors and they did not actually match your DOM structure. Otherwise everything works fine in current jquery.

Harry
  • 87,580
  • 25
  • 202
  • 214
taswyn
  • 4,463
  • 2
  • 20
  • 34