Thanks a lot ricardo..
$(':contains("History")','tr').css("background-color","red");
(WORKS)
One thing I understood was
:contains uses the context to search in it children.not in the same
element.becoz when I use 'td' as the context td has the text 'History' in it
that I am looking for. Is this is a right statement???
Also Can you do the same with filter function...Filter passes the 'this'
which I assume to be element 'TR' here....
$('tr ').filter(function(){
return $(':contains("History")',this);
}).css("background-color","red");
This ends up making all rows red ...What is it I am not seeing....
THanks once again...
ricardobeat wrote:
>
>
> $('tr:contains(Comedy)') and $(':contains(Comedy)','tr') are not the
> same.
>
> When using "tr" as a context, you're searching it's children (the
> tds). So if you pass "td" as a context, the search will happen on it's
> children (which are none in your page). You need
>
> $('td:contains(Comedy)') or to exemplify the relationship, $
> ('td:contains(Comedy)','tr')
>
> - ricardo
>
> On Dec 20, 7:04 pm, "[email protected]"
> <[email protected]> wrote:
>> Or rather the question should be
>>
>> can the :contains selector have a context for its search......and if I
>> make the 'this' to be it will it work.....
>>
>> On Dec 20, 1:43 pm, "[email protected]"
>>
>> <[email protected]> wrote:
>> > Hi,
>> > I am trying to match the content in 'td' and think the problem is with
>> > contains when it is trying to query withing 'td'....
>>
>> > $(":contains('Comedy')",'td').css("background-
>> > color","green");
>> > doesn't work...
>>
>> > but if I tried...
>> > $(":contains('Comedy')",'tr').css("background-
>> > color","green");
>>
>> > this WORKS ....WHY....????
>>
>> >http://chinnakaruppan.googlepages.com/index.html
>>
>> > Please find below the full code...
>>
>> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>> > "http://www.w3.org/TR/html4/loose.dtd">
>>
>> > <html xmlns="http://www.w3.org/1999/xhtml">
>> > <head>
>> > <meta name="generator" content=
>> > "HTML Tidy, seewww.w3.org" />
>> > <meta http-equiv="Content-Type" content=
>> > "text/html; charset=utf-8" />
>>
>> > <title>New Web Project</title>
>> > <style type='text/css'>
>>
>> > </style>
>> > <script type="text/javascript" src="/lib/jquery/jquery.js">
>> > </script>
>> > <script type="text/javascript">
>> > $(document).ready(function(){
>>
>> > $('tr td').filter(function(){
>> > return $(':contains("History")',this);
>> > }).css("background-color","red");
>>
>> > $("td:contains('Tragedy')").css("background-color","blue");
>> > $('tr td').filter(":contains('Comedy')").css("background-
>> > color","green");
>>
>> > })
>>
>> > </script>
>> > </head>
>>
>> > <body>
>> > <table>
>> > <tr class="header">
>> > <th>Title</th>
>>
>> > <th>Category</th>
>> > </tr>
>>
>> > <tr>
>> > <td>As You Like It</td>
>>
>> > <td>Comedy</td>
>> > </tr>
>>
>> > <tr>
>> > <td>All's Well that Ends Well</td>
>>
>> > <td>Comedy</td>
>> > </tr>
>>
>> > <tr>
>> > <td>Hamlet</td>
>>
>> > <td>Tragedy</td>
>> > </tr>
>>
>> > <tr>
>> > <td>Macbeth</td>
>>
>> > <td>Tragedy</td>
>> > </tr>
>>
>> > <tr>
>> > <td>Romeo and Juliet</td>
>>
>> > <td>Tragedy</td>
>> > </tr>
>>
>> > <tr>
>> > <td>Henry IV, Part I</td>
>>
>> > <td>History</td>
>> > </tr>
>>
>> > <tr>
>> > <td>Henry V</td>
>>
>> > <td>History</td>
>> > </tr>
>> > </table>
>> > </body>
>> > </html>
>
>
--
View this message in context:
http://www.nabble.com/problem-with-%3Acontains--in-%27td%27-tp21108297s27240p21110837.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.