My jQuery is not working as expected:
I am trying 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 disable the link defined in a td with class “ms-vb-icon2” within the same row.
$("tr:has(td:contains('test1')) td.ms-vb-icon.a#click").click(function() { return false; });
My code is as below…
<table class='ms-listviewtable'>
<tr>
<td class='ms-vb2-icon'>
<A onclick='GoToLink(this);return false;' href='http://www.google.com' target='_self'><IMG alt='Edit' src='http://web-hub.net/wiki/skins/largepublisher/icon_edit_small.gif'/></A>
</td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'>test1</td>
</tr>
<tr>
<td class='ms-vb2-icon'>
<A onclick='GoToLink(this);return false;' href='http://www.google.com' target='_self'><IMG alt='Edit' src='http://web-hub.net/wiki/skins/largepublisher/icon_edit_small.gif'/></A>
</td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'>test2</td>
</tr>
<tr>
<td class='ms-vb2-icon'>
<A onclick='GoToLink(this);return false;' href='http://www.google.com' target='_self'><IMG alt='Edit' src='http://web-hub.net/wiki/skins/largepublisher/icon_edit_small.gif'/></A>
</td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'></td>
<td class='ms-vb2'>test3</td>
</tr>
</table>
In this case remove the
., the#clickand add the missing2to thevb2, like this:Or alternatively, remove the initial click handler, this seems to be more what you’re after:
You can try out a demo here
You need to remove the already attached event handler because it’ll happen before this new one, since it was attached first. The
.comes out because it’s not a class, it’s a child element, and the#clickcomes out because we’re not looking for an<a id="click">. I think what you intended here wasa[onclick], the has-attribute selector, but since there’s only one link there’s no need for that here 🙂