Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make entire <tr> in a table clickable like <a href="">

Here is my Fiddle

How can I make the entire <tr> as clickable.

In the fiddle all the elements like text and images are clickable, but i want to make the entire <tr> clickable. I won't want to make the jQuery query action, as it won't show the href icon while hovering it.

How can I do this ?

I read this question but it still uses jQuery on click event, which won't show the href icon while hovering it

Here is my HTML

<table border=1>
  <tbody>

    <tr>
      <td style="display: none;">
        13467.36232877521
      </td>
      <td style="display: none;">
        0
      </td>
      <td width="5%" >
        <a href="http://localhost/greenhopping/store/976" style="text-decoration:none;color:black">
          <img src="http://greenhoppingbucket.s3.amazonaws.com/store/profile/1458470633N4IjGniw81.png" alt="image" height="58px" width="58px" style="margin: -8px 0px -9px -7px;" />
        </a>
      </td>
      <td width="25%">
        <div class="semibold">
          <a href="http://localhost/greenhopping/store/976" style="text-decoration:none;color:black">
                                                                            Juice Generation Demo 1  
                                                                        </a>
        </div>
        <div class="text-muted"><i class="fa fa-heart yellow"></i> 0</div>
      </td>
      <td width="35%">
        <div class="text-muted">
          <a href="http://localhost/greenhopping/store/976" style="text-decoration:none;color:black">
                                                                            Juice Generation, 9th Avenue, New York, NY, United States
                                                                        </a>
        </div>
      </td>
      <td width="35%" class="text-right">
        <a href="http://localhost/greenhopping/store/976" style="text-decoration:none;color:black">
          <img src="http://greenhoppingbucket.s3.amazonaws.com/tip/1456942351fQoyY8DNZd.png" alt="image" height="36px" width="40px" />
        </a>
      </td>
    </tr>
  </tbody>
</table>
like image 241
SA__ Avatar asked Jan 23 '26 04:01

SA__


2 Answers

A combination of the above should do the trick.

  1. Add recognizable class to your element.

    <tr class="clickable" data-href="http://website/your_href"></tr>
    
  2. Write CSS for the element to appear clickable.

    .clickable {
        cursor: pointer;
    }
    
  3. Make the thing clickable using Javascript:

    var elements = document.getElementsByClassName('clickable');
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        element.addEventListener('click', function() {
            var href = this.dataset.href;
            if (href) {
                window.location.assign(href);
            }
        });
    }
    
like image 174
minitauros Avatar answered Jan 24 '26 19:01

minitauros


Try putting display as block.

td a { 
   display: block; 
}

Fiddle

Also have a look at answer in this question too.

like image 31
Shashank Avatar answered Jan 24 '26 18:01

Shashank