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?

3
  • Part of the solution: You can search for text in links with this method: stackoverflow.com/questions/926580/… Commented Jun 14, 2010 at 13:45
  • I think that words 'link text' make some confusion here. Do you mean href attribute or text inside <a></a>? Commented Jun 14, 2010 at 13:54
  • Sorry if im confusing, i meant the text inside <a></a> Commented Jun 15, 2010 at 8:57

7 Answers 7

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/

Sign up to request clarification or add additional context in comments.

3 Comments

+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.
Darn, I mistook myself, important_column is used.
@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 ;-)
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");

Comments

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.

Comments

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

Comments

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

Comments

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"

3 Comments

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: 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...
@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.
0
jQuery('.need_link_here a',
    jQuery('.important_column a:contains(IMPORTANT INFO)').parent() 
).attr('href');

2 Comments

OP wrote 'if the link text in "important_column" equals the thing im looking for' but you just look at the class.
@jAndy: Well, that's a very vague criticism (which doesn't suggest why it is 'terrible' or what could be done to improve it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.