Search code examples
javascriptjquerytwitter-bootstrapeventsbootstrap-table

How get td click event from bootstrap table?


I am trying attribute a event to cells of differents columns of my bootstrap table.

The problem is: Although I have set a class to each column, the events attributed to this classes are not fired. I still tried set a class to each div added in cells, but its event also wasn't fired.

So, how can I get a td event using bootstrab table?


Solution

  • Here is the complete code for td click event for bootstap table

    $(document).ready(function(){
      $('table td').click(function(){
         alert($(this).html());
      });
      
      });
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container">
      
      <p>Click on the td below</p>            
      <table class="table">
        <thead>
          <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Email</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>John</td>
            <td>Doe</td>
            <td>john@example.com</td>
          </tr>
          <tr>
            <td>Mary</td>
            <td>Moe</td>
            <td>mary@example.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@example.com</td>
          </tr>
        </tbody>
      </table>
    </div>
    
    </body>
    </html>