views:

84

answers:

7

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 im looking for.
Get the link text in "need link_here" column. The text between <a></a>
How to do in JQuery?

A: 
jQuery('.need_link_here a',
    jQuery('.important_column a:contains(IMPORTANT INFO)').parent() 
).attr('href');
David Dorward
OP wrote 'if the link text in "important_column" equals the thing im looking for' but you just look at the class.
Peter Jaric
terrible jQuery style, IMO
jAndy
@snowlord: Good catch. Corrected it.
David Dorward
@jAndy: Well, that's a very vague criticism (which doesn't suggest *why* it is 'terrible' or what could be done to improve it).
David Dorward
+1  A: 
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
A: 

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
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.
David Dorward
@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
@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.
David Dorward
+1  A: 

piece of js taht would do the trick:

if($('.important_column a').text() == 'SOME TEXT YOU WANT')
{
   $('.important_column a').attr('href', $('.need_link_here a').attr('href'));
}
Lukasz Dziedzia
+1  A: 

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
+1  A: 

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
+4  A: 

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
+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
Darn, I mistook myself, important_column *is* used.
Peter Jaric
@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
@Andy E's head: :)
Peter Jaric
Ah, nice. didnt know about :contains :) thanks!
heffaklump