How can I get the value of current td column value on KeyPress method given in javascript

I am trying to get the current column value in keypress event given in javascript. Here is the code

 $(document).ready(function () {
  $("td").keypress(function (e) {
            var tr = $(this).parents('tr');
            var rownumber = tr.index();
            var colname = $(this).attr("class");            
        });
        
        if(colname == 'HolidayHrs')
        {
          var holidayhrs = the column value of the current row
        }
 })
 
 
<table>
  <tr>
    <td class="HolidayHrs"><a href="#" data-pk="HolidayHrs">@Model.attendanceLogList[i].HolidayHrs</a><input  asp-for="@Model.attendanceLogList[i].HolidayHrs" type="hidden" class="bros"/></td></td>
  </tr>
</table>

2 thoughts on “How can I get the value of current td column value on KeyPress method given in javascript”

  1. If you want to get html code of td,you can use:

    var holidayhrs=$(this).html();
    

    If you only want to get the text of <a> link,you can use:

    var holidayhrs = $(this).find("a:eq(0)").text();
    

    Here is a demoļ¼š

    html:

     <table>
            <tr>
                <td class="HolidayHrs"><a href="#" data-pk="HolidayHrs">HolidayHrs</a><input name="HolidayHrs"  class="bros" /></td>
            </tr>
        </table>
    

    js(put if into function of keypress):

    $(document).ready(function () {
            $("td").keypress(function (e) {
                var tr = $(this).parents('tr');
                var rownumber = tr.index();
                var colname = $(this).attr("class");
                if (colname == 'HolidayHrs') {
                    var holidayhrs1 = $(this).html();
    
                    var holidayhrs = $(this).find("a:eq(0)").text();
    
                }
            });
    
            
        })
    

    result:
    enter image description here

    Reply

Leave a Comment