Skip to content Skip to sidebar Skip to footer

Identifying A Child Element Of A Clicked Target In JQuery

I'm trying to work out how I can right-click anywhere in the table, which has an event listener on the tr tag, and will pass the click() command down to the child button.

Solution 1:

You need to use jquery find method wich searches for first child instance at any depth

jQuery(".catcher").contextmenu(function(e) {
  $(this).find('button').click();
  return false;
});
table {
  border: 1px solid red;
  width: 200px;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="catcher">
    <td>
      <span>
          <button onclick="alert('it works!');">Button</button>
      </span>
    </td>
  </tr>
</table>

Solution 2:

jQuery(".catcher").contextmenu(function (e) {
    //e.target.**FIND CHILD BUTTON**.click();
  $(e.target).find('button').click()
});
table {
  border: 1px solid red;
  width: 200px;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="catcher">
    <td>
      <span>
          <button onclick="alert('it works!');">Button</button>
      </span>
    </td>
  </tr>
</table>

wrap e.target in $() then use .find('button')


Solution 3:

Refer this here Qty is Class Name

($(e).parent().parent().find(".Qty").val()

Post a Comment for "Identifying A Child Element Of A Clicked Target In JQuery"