0

I'm trying to select value from a hidden input inside a td and using this jQuery, but it hasn't worked:

 $('.job-detail').click(() => {
   alert($(this).find('input[name=id]').val());
 });

<tr class="job-detail">
    <td><input name="id" type="hidden" value="..." />1</td>
    <td><input class="name" type="hidden"
        value="..." ></td>
    <td>every day 1:00</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
</tr>
<tr class="job-detail">
    <td><input name="id" type="hidden" value="..." />2</td>
    <td>...</td>
    <td>every day 1 hour</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
</tr>

Can anyone help me please :D Btw, I can not understand clearly 'this' of jQuery until now :(

Nguyen Hoang Vu
  • 369
  • 5
  • 19
  • Duplicate of https://stackoverflow.com/questions/49478312/jquery-detect-td-id-on-click/49478670#49478670 – GSSwain Mar 26 '18 at 11:16

5 Answers5

2

Stick to normal function expression instead of Arrow function, when you want to use this as it does not have its own this and it lexically bind their context so this actually refers to the originating context (scope in which the function was declared)

 $('.job-detail').click(function(){
   alert($(this).find('input[name=id]').val());
 });
Satpal
  • 126,885
  • 12
  • 146
  • 163
2

You can use arrow function but you can't use this keyword inside it, Below is the code which uses arrow function

 $('.job-detail').click((e) => {
   alert($(e.currentTarget).find("input[name=id]").val());
 });
nerding_it
  • 608
  • 5
  • 14
0

The probelm is your arrow function. Either replace it with a 'normal' function or use the arguments of the click function.

To read more about the different types of functions have a look at this question

NielsNet
  • 738
  • 7
  • 11
0

Your HTML is not valid you should add tr into a table tag, Also run your JS inside $(document).ready to make sure DOM object is loaded.

And for this it will refer to curent tr you click.

$(document).ready(function() {
  $('.job-detail').on('click', function() {
    alert($(this).find('input[type=hidden]').val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="job-detail">
    <td><input name="id" type="hidden" value="111" />1</td>
    <td><input class="name" type="hidden" value="..."></td>
    <td>every day 1:00</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
  </tr>
  <tr class="job-detail">
    <td><input name="id" type="hidden" value="222" />2</td>
    <td>...</td>
    <td>every da 1 hour</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
  </tr>
</table>
ElasticCode
  • 5,312
  • 2
  • 22
  • 35
0

The .closest and .find selectors are complements of each other and used together are the best way to get to the corresponding element of where the click (or any event) occurred link.

$('.job-detail').click((e) => {
   alert($(event.target).closest('tr.job-detail').find('input[type=hidden]').val());
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tr class="job-detail">
    <td><input name="id" type="hidden" value="..." />1</td>
    <td><input class="name" type="hidden"
        value="..." ></td>
    <td>every day 1:00</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
</tr>
<tr class="job-detail">
    <td><input name="id" type="hidden" value="..." />2</td>
    <td>...</td>
    <td>every day 1 hour</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
    <td>yyyy/mm/dd hh24/mi/ss</td>
</tr></table>
Narendra Jadhav
  • 8,799
  • 15
  • 28
  • 38