70

I can hack this myself, but I think bootstrap has this capability.

rjurney
  • 4,824
  • 5
  • 41
  • 62

7 Answers7

69

Using jQuery it's quite trivial. v2.0 uses the table class on all tables.

$('.table > tbody > tr').click(function() {
    // row was clicked
});
Community
  • 1
  • 1
Terry
  • 14,099
  • 9
  • 56
  • 84
  • 22
    If you want to bind the click event only to the table body rows then use the '.table > tbody > tr' selector. – miguelcobain Jun 20 '12 at 15:33
  • How to do this with specific table? Like i have 3 tables and i want to bind on click event on one table row then how am i get to do it – Muneem Habib Mar 18 '15 at 05:49
  • 2
    @MuneemHabib just add id to that table and use the id selector `$('#my_id > tbody > tr')` – stefita May 26 '15 at 23:32
  • 9
    Use `.on('click', function(){}); ` instead of `.click` – Maurice Oct 14 '15 at 17:13
  • 2
    @MaHo Perhaps you can qualify why? – troelskn Mar 30 '16 at 07:30
  • Readability and/or personal preference. Both will work as it is a shortcut for `.on(click, handler).` And to be honest, when i typed that comment, i thought .click was deprecated. But i got that mixed up with `.live() `. – Maurice Mar 30 '16 at 10:10
  • @troelskn The reason why would be because with `.on`, if the table rows are pulled dynamically over AJAX or created in Javascript after the click handler loads, then you can still intercept it. It's akin to the deprecated but useful `.delegate` handler that's now part of the `.on` handler if specified properly. – Volomike Nov 20 '20 at 16:26
57

There is a javascript plugin that adds this feature to bootstrap.

When rowlink.js is included you can do:

<table data-link="row">
  <tr><td><a href="foo.html">Foo</a></td><td>This is Foo</td></tr>
  <tr><td><a href="bar.html">Bar</a></td><td>Bar is good</td></tr>
</table>

The table will be converted so that the whole row can be clicked instead of only the first column.

Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
  • nice work with your fork. Please adjust the example how to use twitter-bootstrap and your bootstrap version for this question. – CSchulz Feb 15 '14 at 14:25
  • Include both `bootstrap.css` and `jasny-bootstrap.css` and load `jasny-bootstrap.js`. For the use of rowlink see http://jasny.github.io/bootstrap/javascript/#rowlink – Arnold Daniels Feb 16 '14 at 01:00
  • 2
    It seems that the accepted answer is simpler since most projects already use jQuery. Why double the upvotes here. No offense, just trying to understand why is this solution better? – user Mar 15 '14 at 09:21
  • 2
    @buffer The small plugin also handles triggering the click of the link and styling the row to make it clear it works as a link. Sure, it's a trivial thing which a lot of developers could easily create themselves. The same could be said for Bootstrap's Collapse and Button plugins. – Arnold Daniels Mar 16 '14 at 20:33
  • @Jasny-ArnoldDaniels - your example on http://jasny.github.io/bootstrap/javascript/#rowlink only has first column clickable using Chrome ... doesn't seem to be working – Matt Byrne Apr 11 '14 at 00:34
  • @Jasny-ArnoldDaniels Your link seems to be broken, I get a 404 from Github. – Matt Mc Jun 06 '14 at 06:10
25

That code transforms any bootstrap table row that has data-href attribute set into a clickable element

Note: data-href attribute is a valid tr attribute (HTML5), href attributes on tr element are not.

$(function(){
    $('.table tr[data-href]').each(function(){
        $(this).css('cursor','pointer').hover(
            function(){ 
                $(this).addClass('active'); 
            },  
            function(){ 
                $(this).removeClass('active'); 
            }).click( function(){ 
                document.location = $(this).attr('data-href'); 
            }
        );
    });
});
idmean
  • 14,540
  • 9
  • 54
  • 83
Simmoniz
  • 1,080
  • 15
  • 27
6

I show you my example with modal windows...you create your modal and give it an id then In your table you have tr section, just ad the first line you see below (don't forget to set the on the first row like this

<tr onclick="input" data-toggle="modal" href="#the name for my modal windows" >
 <td><label>Some value here</label></td>
</tr>                                                                                                                                                                                   
awesoon
  • 32,469
  • 11
  • 74
  • 99
matouuuuu
  • 69
  • 1
  • 1
3
<tr height="70" onclick="location.href='<%=site_adres2 & urun_adres%>'"
    style="cursor:pointer;">
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
alpc
  • 598
  • 3
  • 6
0

May be you are trying to attach a function when table rows are clicked.

var table = document.getElementById("tableId");
var rows = table.getElementsByTagName("tr");
for (i = 0; i < rows.length; i++) {
    rows[i].onclick = functioname(); //call the function like this
}
Starx
  • 77,474
  • 47
  • 185
  • 261
0

You can use in this way using bootstrap css. Just remove the active class if already assinged to any row and reassign to the current row.

    $(".table tr").each(function () {
        $(this).attr("class", "");
    });
    $(this).attr("class", "active");
Naren
  • 1