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