You could use this piece of code, making use of prev() to find previous sibling cell, and removeAttr() to remove the link from the label:
$('#uniqueID td').each(function () {
if ($(this).text().trim() == "0") {
$(this).prev().find('a').removeAttr('href').css({color: 'grey'});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="uniqueID">
<table>
<tr>
<td><a href="url">Label 1</a></td>
<td>12</td>
<td><a href="url">Label 2</a></td>
<td>0</td>
<td><a href="url">Label 3</a></td>
<td>2</td>
</tr>
</table>
</div>
Chaining is safe
There is no need to add a check whether there really is a previous cell or a link in it, as jQuery will just do nothing in that case (the jQuery objects are empty arrays in that case).
Removing the href
attribute
In this solution the link is made non-clickable by just removing the href
attribute, as apparently it serves no purpose, it is better to get rid of it.
Also realise that without removing the href
attribute -- even when disabling some mouse pointer events -- the URL is still enabled and the user can still follow it via several methods:
the link can get focus (e.g. via tab key), and once it has focus, there are several ways of activating the link:
- With the enter key;
- With the context menu key (often next to right Ctrl key): the link can be opened via that popup menu;
- Several ways exist with other input devices (e.g. TV controls)
Browser plug-ins may help the user with using links in some way;
- Scripts loaded with the page might also use the link in some way....
These are the methods that come to mind, but there might be more.
Because the link is actually there, is might be misleading also for those using screen readers) or other accessibility tools.
See also this question.
Ignoring white-space
The trim() call makes sure that the zero is also recognised even when there is white space around it.