I'm trying to get the href value of an anchor tag using jQuery, but the this keyword is not working as expected.
Here is the result of the code in the console:
The part of the code I have problem with:
$('#container a').click(() => {
console.log(this);
let link = $(this).attr("href");
console.log("from jquery - " + link);
//chrome.tabs.create({ url: link });
});
As you can see, the this keyword is pointing on the window object.
This code is part of an extension I'm trying to build for Opera.


You are using an arrow function
() => {..}instead of a regular functionfunction () {...}, that's why yourthisdidn't work as you've expected.So instead of this:
Use this:
Your updated code:
OR with an arrow function:
More on arrow functions: