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)

})
Woosik
  • 23
  • 1
  • 4
  • 1
    Does this answer your question? [What does arrow function '() => {}' mean in Javascript?](https://stackoverflow.com/questions/29030747/what-does-arrow-function-mean-in-javascript) – Tân Dec 25 '19 at 17:01
  • actually, not. but thx :) – Woosik Dec 25 '19 at 19:33

1 Answers1

1

The jQuery .parent() method return the parent of a span element, which is shown in the documentation.

Your icon is probably inside a span element and when you call the method parent() var p = $(this).parent(); you are targeting the Window object.

kodak
  • 81
  • 11