0

I would like to get result that is clicked DOM element.

When I try below code, it return jQuery object.

my desired result is like this

<td>1</td> ← clicked DOM element

Are there any method to get this?

jQuery($ => {
  $('td').on('click', function() {
console.log($(this));
})
});
td {
padding:5px;
border:solid black 1px;}

table{
border-collapse:collapse;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
imjared
  • 19,492
  • 4
  • 49
  • 72
Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • Does this answer your question? [How to get a DOM Element from a JQuery Selector](https://stackoverflow.com/questions/1677880/how-to-get-a-dom-element-from-a-jquery-selector) – imjared Mar 28 '20 at 03:44

4 Answers4

1

Just change $(this) to this

jQuery($ => {
  $('td').on('click', function() {
console.log(this);
})
});
td {
padding:5px;
border:solid black 1px;}

table{
border-collapse:collapse;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
JohnSnow
  • 444
  • 4
  • 17
1

Just don't wrap this in JQuery.

jQuery($ => {
  $('td').on('click', function() {
console.log(this);
})
});
td {
padding:5px;
border:solid black 1px;}

table{
border-collapse:collapse;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

You can try something like this:

$(document).ready(function() {
  $('td').on('click', function() { /* your click event */
    console.log("you clicked", this);
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
0

Just change console.log($(this)) to console.log(this) in your click event.

Because JQuery using only this not wrapped $(this).

Karmdip joshi
  • 140
  • 2
  • 14