Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

jQuery why is $(this) in callback function "window"?

$
0
0

I was about to make delete button for my to-do list code. this is my code in <script>

  var del = $("<ion-icon name='trash'></ion-icon>").click((e)=>{
    var p = $(this).parent();
    p.remove()
    console.log(this)
  })

and I got log like this in browser console.

Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}

now I found another one is way to use $(e.target) but I'm still curious why $(this) in callback function is "window".

I'm waiting for your great answer :) thx !

  • and this is what I wanna do in my code
$('#task').click((e)=>{

  var task = $("<li class='task'></li>").text($('#enter-task').val())

  var del = $("<ion-icon name='trash'></ion-icon>").click((e)=>{
    var p = $(this).parent();
    p.remove()
    console.log(this)
  })

  task.append(del)

  $("#tasklist").append(task)

})

Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>