Reputation: 3839
This is some event handling for a list of items in my dom.
Using event delegation, the click handler is on the parent container
$( "#list" ).on( "click", "a", (event) => {
event.preventDefault();
event.stopImmediatePropagation();
let text = $(this).parent().children().last().text();
// The following HAS content (seen in the dev tools debugger !!!!)
// $(this).parent().children().last().text()
// But the text variable gets No content - WHY ?
// "" is printed....
console.log(text);
});
Any ideas? Any help is appreciated.
Upvotes: 4
Views: 127
Reputation: 1863
Because you use arrow function and this
in arrow function is window
in browser.
$( "#list" ).on( "click", "a", function (event) {
event.preventDefault();
event.stopImmediatePropagation();
let text = $(this).parent().children().last().text();
debugger
console.log(text);
});
Upvotes: 2