Is anchoring a TR possible?

Asked

Viewed 1,076 times

8

It is possible outside an element tr link some address as below?

<a href="www.enderecodesejado.com.br"></a>

If it is not possible, Javascript would solve this or better forget?

I have the following table as shown in the image below, and I wanted when the user clicked on a line to be directed to an X address.

inserir a descrição da imagem aqui

3 answers

7


Unfortunately there is no specific construction for this. You will have to do "in hand".

It is relatively simple to do with jQuery and data-* Attributes. Example:

Javascript:

$('tr').click(function() {
    window.location.href = $(this).attr('data-href');
});

HTML:

<table>
  <tr>
    <th>
      Empresa
    </th>
    <th>
      CEO
    </th>
  </tr>
  <tr data-href="http://www.google.com/">
    <td>
       Google
    </td>
    <td>
      Larry Page
    </td>
  </tr>
  <tr data-href="http://www.yahoo.com/">
    <td>
        Yahoo!
    </td>
    <td>
      Marissa Mayer
    </td>
  </tr>
  <tr data-href="http://www.microsoft.com/">
    <td>
        Microsoft
    </td>
    <td>
      Steve Ballmer
    </td>
  </tr>
</table>

Most complete example (including style).

  • +1 for "(including style)"

  • Verdade @Zignd! Full answer came. Valeu @Talles

  • To avoid unexpected behavior it is recommended to include the [data-href] on the selector: $('tr[data-href]').click(...).

3

Yes it is possible with javascript. Inserting links between table and tr is not correct syntax, so the path is actually javascript.

You can add an "Event Handler" that looks for clicks within the rows of the table. When a click happens it fetches the link from that line and opens it.

Example with jQuery:

var linhaTabela = $('table tr');
linhaTabela.on('click', function (event) {
    var link = $(this).find('a').attr('href')
    link && window.location = link;
});

Example


If there is no link inside the line as I mentioned in the example above, you can assign via your php the link information in a field data-, yes in the table row.

So my example is

html

<tr data-href="http://www.google.com">

javascript

var linhaTabela = $('table tr');
linhaTabela.on('click', function (event) {
    var link = $(this).data('href');
    link && window.location = link;
});

Example

  • the link would be outside the tr.

  • @Luitame, how on the outside? Can you explain better?

  • Yes! I have one table correct? I have a loop in PHP to print lines (tr) and outside of that tr wanted to put a link. Example: <table> <tbody> <a href='painel.php?spv=nav/userview&usr=1'><tr> Conteudo </tr> </a> </tbody></table>

  • Make like the line (tr) become a hyperlink, right?

  • right!!! @Zignd

3

If you wrap the <tr> in a <a>, your HTML will become invalid. I suggest you place the URL in an attribute data, and use a very similar code to Sergio’s to navigate to the destination.

HTML

<table>
    <tr data-url="http://www.enderecodesejado.com.br">
        <td>...</td>
    </tr>
</table>

Javascript (jQuery)

$('table').on('click', 'tr', function() {
    var url = $(this).attr('data-url');
    if(url) window.location = url;
});
  • Thank you for the strength...

  • +1 because the answer is useful, correct and because I want a medal :)

  • Hehe, thank you @Sergio. I’m in the habit of voting for competing answers in the OS, and I warn you that this medal takes a while to arrive.

  • hehe, yes, so it’s good to get the habit early :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.