All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Get Table Row Data Onclick

Last Updated : Mar 11, 2024

jQuery Get Table Row Data Onclick

In this article we will show you the solution of jQuery get table row data onclick, popular JavaScript library jQuery makes web development easier by offering a variety of tools and functions. Data retrieval from HTML tables is one of jQuery's most frequently utilised features.

By using the "click" event to record when a user clicks on a table row and jQuery to extract the data from that row, this capability may be accomplished.

Although getting table row data on click is a fairly simple process, understanding jQuery syntax and HTML structure is necessary.

Developers may quickly access the data in the table and alter it as necessary by using jQuery selectors. For web applications that involve user input and data management, this functionality is especially helpful.

Step By Step Guide On jQuery Get Table Row Data Onclick :-

<!DOCTYPE html>
<html>
<head>
 <title>Get Table Row Data OnClick using jQuery</title>
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
 <script type="text/javascript">
  $(document).ready(function(){
   $('table tr').click(function(){
    var col1 = $(this).find('td:eq(0)').text();
    var col2 = $(this).find('td:eq(1)').text();
    var col3 = $(this).find('td:eq(2)').text();
    alert("You clicked row with values: " + col1 + ", " + col2 + ", " + col3);
   });
  });
 </script>
</head>
<body>
 <table>
  <thead>
   <tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td>Value 1.1</td>
    <td>Value 1.2</td>
    <td>Value 1.3</td>
   </tr>
   <tr>
    <td>Value 2.1</td>
    <td>Value 2.2</td>
    <td>Value 2.3</td>
   </tr>
   <tr>
    <td>Value 3.1</td>
    <td>Value 3.2</td>
    <td>Value 3.3</td>
   </tr>
  </tbody>
 </table>
</body>
</html>
  1. You can see that we have written the code here for Jquery to retrieve table row data upon click.
  2. We build an HTML file with a table that has three columns & three rows, as well as a script that uses jQuery to obtain information from a particular table row.
  3. The script begins by awaiting the completion of the HTML document's loading and its readiness for manipulation.
  4. The $(document).ready() function is used for this.
  5. Each row (tr>) in the table is given a click event listener by the script after the document is complete. The jQuery selector $('table tr') is used for this.
  6. The event listener is activated and the code is executed when a row is clicked.
  7. Find() is used to choose the three columns in the row, one at a time, with $(this) denoting the clicked row.
  8. Each column is selected by its index using the eq() function: eq(0) selects the first column, eq(1) selects the second, and so on.
  9. Each selected column's text content is retrieved and saved in a variable (col1, col2, and col3) using the text() function.
  10. The row that was clicked and the information it contains are then indicated by an alert message that includes the values of col1, col2, and col3, which is shown.
  11. An alert indicating the values of the three columns in that row appears when the user clicks on a row in the table.

Conclusion :-

As a result, the concept behind jQuery's obtain table row data onclick seemed clear to us.

We also discovered that the code targets all of the tr elements inside of the table element and watches for a click event by using jQuery selectors & the click() event.

When a row is clicked, the code searches for the text content of each td element within that row and shows an alert with the values.

I hope this article on jQuery get table row data onclick helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪