0

I'm using the 'table-hover' in B4, Is there anyway to put an href to the table rows that are being hovered so they are actually clickable?

<table class="table table-hover">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>
2

1 Answer 1

3

Here's one way of doing it. Note that I'm using Javascript here, but using window.location.assign('http://www.google.com'); will do the same thing as "href". Note the single quote, not double.

function hello(text) {
alert(text);
}
.table-hover tr:hover {
background:#00ff00;
}
<table class="table table-hover">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <tr onclick="hello('row 1')">
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr onclick="window.location.assign('http://www.google.com');">
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr onclick="hello('row 3')">
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>

1
  • 1
    Can also add cursor: pointer; to the style if you want the cursor to change to the hand to indicate a clickable area. Oct 14, 2020 at 14:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.