tags:

views:

36

answers:

2

Hello,

I have the following table structure

<table class="ms-listviewtable>
<tr>
<td class="ms-vb2-icon"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2">test1</td>
</tr>
<tr>
<td class="ms-vb2-icon"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2">test2</td>
</tr>
<tr>
<td class="ms-vb2-icon"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2"/>
<td class="ms-vb2">test3</td>
</tr>
</table>

I need to loop through each row then loop through each td and check if a td in a row contains the text 'test1' if so I need hide the td with class "ms-vb2-icon" within the same row.

Can someone please give me some pointers? Many thanks,

A: 
$("td:contains('test1')").addClass("ms-vb2-icon");

?

http://api.jquery.com/contains-selector/
http://api.jquery.com/addClass/

Nikita Rybak
+3  A: 

You can do it like this:

$("tr:has(td:contains('test1')) td.ms-vb2-icon").hide();

You can test the code against your markup here

This uses :contains() to see if a <td> contains that text, wrapped in :has() to see if the the <tr> has an element matching that, the next we're finding the td.ms-vb2-icon cell in the matched rows and hiding them.

Nick Craver
Many thanks for your explaination as well. I can see it working in the fiddle.Just not sure why its hiding the td on all rows in my webpart, unless the html table structure I see in IE developer toolbar isn't the same as the generated html code....
nav
@nav - I had to fix your markup a bit in fiddle, for example `<td>` can't be self-closing and the `<table>` element's `class` needs a closing quote. Make sure your actual markup is valid by checking it here: http://validator.w3.org/
Nick Craver
Thanks Nick i have pasted the actual markup into the fiddle edit http://jsfiddle.net/QUvYW/
nav
sorry to be a pain, but can you spot why it doesnt work as expected in my edited fiddle..?
nav
@nav - You still have invalid markup, all sorts of attributes...but it is still working, at least here in Chrome and IE, comment out the jQuery like this to see the difference: http://jsfiddle.net/QUvYW/1/
Nick Craver
i tried to do something a little different - to look for the td with the text test1 and disable the link on the same row but it doesnt seem to work...i should probably repost this as a seperate question, thanks.http://jsfiddle.net/QUvYW/3/
nav