Add class to table in div containing <a> with specified href
<p>I have a situation where I need to add a class to a table, but only if the table is inside a <strong>div</strong> with id 'mydiv' AND there is a row in table containing an &lt;a href=...&gt; element where the <strong>href</strong> contains the term 'myref', including the quotes. Is this easy in jQuery and if so, how do I do this? I just can't work out how to change an element while referring forward and backwards to others.</p>

<p>I tried the following, but it doesn't seem to work (I am relatively new to jQuery, as you can probably see from the below):</p>

<pre><code>$('.mydiv').find('td').find('a[href]').contains('myref').parent.parent.addClass('myclass')
</code></pre>

<p>The following html snippet simulates my situation (I cannot change the html, just add some javascript and css):</p>

<pre><code>&lt;div id='mydiv'&gt;
    &lt;a href='blahblah'&gt;Some text&lt;/a&gt;
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;a href="some href"&gt;Some text&lt;/a&gt;&lt;/td&gt;
                &lt;td&gt;&lt;a href="javascript:some javascript containing 'myref' in quotes"&gt;Some text&lt;/a&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;a href="some href"&gt;Some text&lt;/a&gt;&lt;/td&gt;
                &lt;td&gt;&lt;a href="javascript:some javascript without match"&gt;Some text&lt;/a&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
&lt;/div&gt;
&lt;div id='otherdiv'&gt;
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;a href="some href"&gt;Some text&lt;/a&gt;&lt;/td&gt;
                &lt;td&gt;&lt;a href="javascript:some javascript containing 'myref' in quotes"&gt;Some text&lt;/a&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
&lt;/div&gt;
</code></pre>

<p>The first table needs to get the new class, the second and third don't.</p>