Search HTML table with jQuery

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" is equal to what it is looking for. Get the link text in the "need link_here" column. Text Between <a></a>
How to do it in jQuery?

+3
source share
7 answers

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

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

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

+4
source
if ($("table tr td.important_column a[href=/bla/blah/link]").length) {
   var link = $("table tr td.need_link_here a").attr("href");
}

changes

$("td.need_link_here a", $("table tr td.important_column a[href=/bla/blah/link]").closest(tr)).attr("href");
+1
source

js taht :

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

- ?

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

.

+1

, OP. ?

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

You can use the following method to get the siblings of the element found:

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

will select the href of the second td element after the symbol class = "important_column"

0
source

Source: https://habr.com/ru/post/1749930/


All Articles