views:

89

answers:

2

HTML:

<table>
 <tr>
  <td>
   <a href="#" class="nav">link</a>
  <td>
 </tr>
</table>

I want to: FIND TEXT 'link' in a.nav and ADD ID "abc" to 'table'. Tried this but it doesn't work:

$('table>tbody>tr>td>a.nav:contains("Forum Index")').parents('table').attr('id', 'newID');

(tbody because most browsers add it automatically)

+2  A: 

Your selection criteria does not need to be so explicit. In fact I would make them as simple as possible while still selecting only the elements that you need. Simple selection criteria will be more performant.

$('a.nav:contains("link")').parents('table').attr('id', 'abc');
joshperry
+1  A: 

Yes, it does work. I tried it with only changing the string in the contains from "Forum Index" to "link", and it does change the id of the table.

However, you shouldn't really change the id of elements. Use a class intead if possible.

Guffa