3

I have a table and need a specific column in a specific row.

<tr></tr>
<tr>
    <td></td>
    <td></td>
    <td class="important_column"><a href="/bla/blah/link">IMPORTANT INFO</a></td>
    <td></td>
    <td class="need_link_here"><a href="/I/WANT/THIS/LINK/">link</a></td>
</tr>
<tr></tr>

So if the link text in "important_column" equals the thing I'm looking for.
Get the link text in "need link_here" column. The text between <a></a>
How to do in jQuery?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
heffaklump
  • 1,526
  • 3
  • 21
  • 27

7 Answers7

4

You can use :contains() to check to see if an element contains specific text:

$("table td:contains('IMPORTANT INFO')")
    .next().next()
    .children("a");

EXAMPLE - http://jsfiddle.net/Kkywt/

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • +1 because this is the only answer so far that doesn't use the important_column and need_link_here classes which I doubt will be present in the real situation. – Peter Jaric Jun 14 '10 at 17:16
  • Darn, I mistook myself, important_column *is* used. – Peter Jaric Jun 14 '10 at 17:58
  • 2
    @snowlord: it is, but it doesn't have to be. The code here would work just as well without it. In fact, I just edited the answer and the example so that you could feel like your +1 was justified ;-) – Andy E Jun 14 '10 at 18:14
1
if ($("table tr td.important_column a[href=/bla/blah/link]").length) {
   var link = $("table tr td.need_link_here a").attr("href");
}

variation

$("td.need_link_here a", $("table tr td.important_column a[href=/bla/blah/link]").closest(tr)).attr("href");
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
1

Piece of js that would do the trick:

if($('.important_column a').text() == 'SOME TEXT YOU WANT')
{
   $('.important_column a').attr('href', $('.need_link_here a').attr('href'));
}
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
dzida
  • 8,854
  • 2
  • 36
  • 57
1

Something like this?

$(function() {
  column_text = $(".important_column a").text();
  if(column_text == "IMPORTANT INFO") {
    link_href = $(".need_link_here a").attr("href");
    alert(link_href);
  }
});

Check out this working demo.

Jeriko
  • 6,547
  • 4
  • 28
  • 40
1

It's really not clear what the OP wants. Is this close?

$('#myTable tr').each(function ()
{
    var $tr = $(this);
    if($tr.find('td.important_column').text() === 'IMPORTANT INFO')
    {
        alert($tr.find('td.need_link_here > a').attr('href'))
    }
});
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0
jQuery('.need_link_here a',
    jQuery('.important_column a:contains(IMPORTANT INFO)').parent() 
).attr('href');
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • OP wrote 'if the link text in "important_column" equals the thing im looking for' but you just look at the class. – Peter Jaric Jun 14 '10 at 13:42
  • @jAndy: Well, that's a very vague criticism (which doesn't suggest *why* it is 'terrible' or what could be done to improve it). – Quentin Jun 14 '10 at 13:58
0

You can use the next method to get siblings of a found item:

$('.important_column').next().next().attr("href")

will select the href of the 2nd td element after the one with class="important_column"

Marks
  • 3,613
  • 5
  • 31
  • 46
  • 1. Assumes that class is not significant. 2. Doesn't check the text. 3. Tries to get the value of the href attribute of the TD element. – Quentin Jun 14 '10 at 13:59
  • @David Dorward: Isn't it quite obvious that "need_link_here" will not be present in the actual page? If it was, it would be quite trivial... – Peter Jaric Jun 14 '10 at 17:57
  • @snowlord: No, it isn't obvious, the question is open to quite a lot of interpretation. It isn't even clear if the "important_column" class would be there. – Quentin Jun 14 '10 at 19:02