Is this JavaScript code or PHP code? The use of $category as a variable
name, along with the attempt to interpolate the variable directly in the
string in the first example, makes me wonder if you're actually talking
about PHP code here. You can use $ as part of a variable in JavaScript, but
it's unconventional (except that in jQuery code, it's common practice to use
a $ prefix for a variable that con

But let me assume we're talking JavaScript. jQuery's .filter() method, being
JavaScript code, does not know or care whether its string argument started
life as a string literal, or a variable, or an expression containing some
mix of string variables and literals. In other words, it works exactly the
same as any other JavaScript function. There is no special string syntax for
the .filter() method or any other jQuery method.

Also note that you can't interpolate a variable into a string using the
$variablename notation as you would in PHP. In JavaScript, you use + to
concatenate strings (either string literals or variables), and you can use
either ' or " as the delimiter for a string literal.

So, this code excerpt:

    ...filter(':contains(JavaScript)')

and this one:

    var category = 'JavaScript';
    ...filter( ':contains(' + category + ')' )

will work identically.

Let's look at the other things you tried. I'll list each one and right below
it the actual string the code passed to the filter function.

    ...filter(':contains($category)');

    :contains($category)

Because JavaScript doesn't have PHP-style interpolation, $category is simply
part of the string.

    ...filter(':contains("+ $category +")');

    :contains("+ $category +")

Yes, that's all one string literal with double quotes and plus signs inside
it. You were almost on the right track; if you'd used either single quotes
or double quotes all the way through, it would have worked.

    ...filter(':contains(" \' " + $category " \' ")');

    :contains(" \' " + $category " \' ")

Again all one string literal. Because the entire string was delimited with
single quotes, the double quotes were simply part of the string. And you
escaped the single quotes so they also became part of the string, along with
the + operators and the variable name.

So, all that said, here's another tip for next time. As I mentioned, the
.filter function doesn't have any special string syntax of its own. It's
just JavaScript code. So, you can test your string expressions by using them
in other contexts. Instead of calling .filter and wondering why it doesn't
work, test the string expressions with alert() function calls, or using
Firebug with console.log() calls. Then you can easily see the actual result
of any string expression.

-Mike

> -----Original Message-----
> From: jquery-en@googlegroups.com 
> [mailto:jquery...@googlegroups.com] On Behalf Of precar
> Sent: Friday, December 26, 2008 8:38 AM
> To: jQuery (English)
> Subject: [jQuery] :contains working with text, but not with variable
> 
> 
> Hi guys,
> 
> I've already read the other thread on this topic and tried 
> the following versions of the statement:
> 
> $sites = $(data).find('categories').filter(':contains($category)');
> $sites = $(data).find('categories').filter(':contains("+ $category +")');
> $sites = $(data).find('categories').filter(':contains(" \' " + $category "
\' ")');
> 
> (The variable is $category, and it contains plain text.)  All 
> of these return no results.  When I use text:
> 
> .filter(':contains(JavaScript)')
> 
> the statement works, so I know it's not the rest of the 
> statement that's the problem.
> 
> Can someone shed light on what the proper syntax is for using 
> :contains with a variable name?
> 
> 
> Thank you,
> Precar.
> 

Reply via email to