This is basic javascript string manipulation, not jQuery.
Below is a very (very) quick precis of strings in javascript. It may
help, it may not.

A string is defined by it's surrounding quotes, be they (paired)
single or double.
You can concatenate strings by using the + operator.
A string can be a literal ....
'This is a string'
 .... or contained in a variable ....
var str = 'Another string';

Concatenation works with literal strings and variables alike....
var example = 'One string, ' + "Two strings";
  // example = One string, Two strings
var str = 'Another string';
var example = 'One string, ' + str;
  // example = One string, Another string
var str = "Double quotes";
var example = 'Single quotes or ' + str;
  // example = Single quotes or Double quotes
But...
var str = 'is a string';
var example = 'This str';
  // example = This str
  // ... the string limits are defined by the single quotes so the
fact that str happens to exist as a variable is totally immaterial
  // see PHP note at end

Concatenation also works with anything that returns a string (such as
a function) or with numbers (Javascript automatically type-casts it to
a string if possible)....
function getStr(){ return 'string number'; }
var num = 4;
var example = 'This is ' + getStr() + ' ' + num;
  // example = This is string number 4

A string defined within single quotes can contain double quotes, and
vice versa....
"I'm a string"
var str = 'Fred said "So am I!".';

To include a single quote within a single-quoted string it needs to be
'escaped' by preceding it with a backslash....
var str = 'This won\'t fail';
var fail = 'This would've failed!';
// string is taken as 'This would' and it doesn't know what to do with
the rest of it

Same applies for double-quoted strings...
var str = "It's called a \"String\"...";
var fail = "It's not called "Fred"."
// first string is "It's not called", doesn't know what to do with
Fred, second string would have been "." (IF it had known what to do
with Fred!)

[Note : there could be complications here regarding double (or
sometimes more) escaping that I'm not going to go into - it can get
too convoluted.]

PHP:
If you're used to working with PHP (and maybe some others) then you
will know that PHP allows you to this...
$str = 'a string';
$example = "This is $str called 'Fred'.";
  // $example = This is a string called 'Fred'.
You can't do this with (native) Javascript!

jQuery:
A selector expression is a string. The fact that certain selector
formats can contain quotes does not change this.
This means that the selector can be a string literal, a string
variable, or any constructed string (by means of the concatenation
methods described above, for example).
If you need to construct a selector from variables then you simply
need to follow the concatenation rules for string and quotes.
So....
// hide element with id myDiv ...
var selector = '#myDiv';
$( selector ).hide();

// hide elements within #myDiv that have class of myClass ...
var str = 'myClass';
// concatenate to get a selector of '#myDiv .myClass' ...
$( '#myDiv .' + str ).hide();

// hide div elements within #myDiv that contain the text 'fred' ...
var str = 'fred';
// concatenate to get a selector of '#myDiv div:contains(fred)' ...
$( '#myDiv div:contains(' + str + ')' ).hide();

// hide any element containing the text 'foo bar' ...
var str = 'foo bar';
// concatenate to get selector of ':contains("foo bar")' ...
$( ':contains("' + str + '")' ).hide();
// uses the double quotes within the contains() - it needn't but it
does.

An alternative could be...
var str = '"foo bar"'; // double quotes are in this variable!
// concatenate to get selector of ':contains("foo bar")' ...
$( ':contains(' + str + ')' ).hide();

Or simply ...
var str = 'foo bar';
// concatenate to get selector of ':contains(foo bar)' ...
$( ':contains(' + str + ')' ).hide();

Or even ...
var str = 'foo bar';
var selector = ':contains(' + str + ')';
// selector is ':contains(foo bar)' ...
$(selector).hide();


I think (hope) I have managed to avoid too many typos or glaringly
obvious errors.
Please take into account that this is only a very basic overview.


On Nov 13, 6:12 am, jeff <[EMAIL PROTECTED]> wrote:
> I am a little confused on the syntax for :contains.  I want to be able
> to pass in a variable to the parameter for :contains.  It can take
> either quoted or non-quoted text, which on one hand can be convenient,
> but on the other is difficult if you are trying to pass a variable.
>
> Here is the test cases I came up with:
> (all sections are meant to be commented out except the one being
> tested)
>
>         // _______ adding a string directly into :contains ________
>         var $elements = $('a:contains(farm)');      // works fine: finds all
> links that contain the text "farm"    var $elements = $
> ('a:contains("farm")');    // works fine
>
>         // ________ adding a regular string variable to :contains _________
>         var searchText = "farm";
>         var $elements = $('a:contains(searchText)');           // doesn't work
> (can't distinquish between a variable and text) var $elements = $
> ('a:contains(String(searchText))');     // doesn't work (I thought I would
> try)
>         var $elements = $('a:contains("+searchText+")');       // doesn't
> work
>         var $elements = $('a:contains('+searchText+')');               // 
> works!
>
>         // __________ adding a jQuery object to :contains ____________
>         $('#search-box').keyup(function() {
>                 var $searchText = $('#search-box').val();
>                 var $searchResults = $('a:contains($searchText)');        // 
> does
> not work
>                 var $searchResults = $('a:contains('+$searchText+')');   // 
> works!
>         });
>
> So, I figured out a solution, but I am not even clear why it works (I
> found an example buried in these forums.)  It seams that if :search()
> required quoted parameters and unquoted parameters implied a variable,
> then it would be much more intuitive.  I am somewhat new to jQuery. It
> is amazing and actually fun to program in. But I am not that familiar
> with all of the subtleties.
>
> Can anybody tell me why this solution works? Also, it would be great
> if someone could update the documentation to provide an example
> of :contains() that has a variable being passed in.
>
> thanks

Reply via email to