All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get TD Value In jQuery

Last Updated : Mar 11, 2024

How To Get TD Value In jQuery

In this article we will show you the solution of how to get td value in jquery, here we are going to show you example to achieving result with the help of on() and text() method.

The on() method attaches one or more event handlers to the selected and child elements. Then text() method used to retrieves table cell value by appending with 'this' keyword. By using this concept you can get dynamic cell values for each clicks from html table data.

Step By Step Guide On How To Get TD Value In jQuery :-

Here we defined html block with table, which contains border and id attributes then three rows of data. In script block we defined ready method, which is loads source code on browser.

Within that we need to specify table each cell, so we defined accurately and using this keyword you can collect right cell value when user click on any table cell. At last retrieved answer of cell value displayed on console panel.

<!DOCTYPE html>
<html>
<head>
  <title>table cell value in jQuery</title>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
  $(document).ready(function () {
        $('#tbl tbody td').on('click', function () {
          var vl = $(this).text();
          console.log(vl);
        });
      });
    </script>
</head>
<body>
    <table id="tbl" border="1">
          <tr><td>1</td><td>Sachin</td><td>Tendulakar</td></tr>
          <tr><td>2</td><td>Rohit</td><td>sharama</td></tr>
          <tr><td>3</td><td>Virat</td><td>Kohli</td></tr>
    </table>
</body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready function, there you need to point table cell accurately. For that we specified using "#tbl tbody td" then need to bind that with on(). To load click event whenever clicks on table cell.
  8. Now you points out exact column value, this keyword helps you to collect clicked cell value by user. Result stored on variable "vl" then displayed on console panel.
  9. In body block we defined html table with three rows of data, you can modify this as per your choice result will get affect by your changes.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get table cell value using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes in document without any error.

When we executes this program on browser we can see table with data, now user needs to click on any column from table that cell value displayed on console panel.

To verify your result use "ctrl+alt+j" on browser console panel will open with result.

I hope this article on how to get td value in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪