tags:

views:

35

answers:

3

Hi,

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; });

http://jsfiddle.net/R8fuR/

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'/&gt;&lt;/A&gt;
</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'/&gt;&lt;/A&gt;
</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'/&gt;&lt;/A&gt;
</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>
A: 

In your selector there is an extra dot between the td class and the anchor. Try:

$("tr:has(td:contains('test1')) td.ms-vb-icon a#click").click(function() { return false; });
Mark B
A: 

should look like:

$('table.ms-listviewtable > tbody').find('td').each(function(){
   var $this = $(this);
   if(/test1/.test($this.text())){
      $this.siblings('.ms-vb-icon2').find('a')
         .unbind('click')
         .removeAttr('onclick')
         .attr('disabled', 'disabled');
   }
});
jAndy
+1  A: 

In this case remove the ., the #click and add the missing 2 to the vb2, like this:

$("tr:has(td:contains('test1')) td.ms-vb2-icon a").click(function() { return false; });

Or alternatively, remove the initial click handler, this seems to be more what you're after:

$("tr:has(td:contains('test1')) td.ms-vb2-icon a")
  .removeAttr('onclick').click(function() { return false; });

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 #click comes out because we're not looking for an <a id="click">. I think what you intended here was a[onclick], the has-attribute selector, but since there's only one link there's no need for that here :)

Nick Craver
Thanks Nick, though it doesn't seem to disable the google link in firefox http://jsfiddle.net/R8fuR/1/
nav
@nav - 2 things, you weren't including jQuery, and there's a missing `2` on the vb2, to eliminate another error we can remove that `onclick` attribute, like this: http://jsfiddle.net/R8fuR/2/ I'll update the answer to match.
Nick Craver
Thanks for spotting the errors that works great
nav