I've got a very simple code
HTML
<a class="qqPopupCTA" href="http://example.com">Button</a>
JS
$qqPopupCTA = $('.qqPopupCTA');
function showForm(e){
e.preventDefault();
var targetPageUrl = $(this).attr('href');
// do stuff
}
$qqPopupCTA.on('click', function(e){showForm(e);});
However I keep getting undefined href. Console.log($(this)) returns "window" and console.dir($(this)) returns "e.fn.init[1]"
Any idea what am I doing wrong? The code is super simple so I must be missing something obvious.
I think I've tried everything.
When you call
showForm(e), the context(this) is not the anchor object insideshowFormit is the window object.So you can just pass the function reference as the click handler
or pass a custom context to the
showFormfunction using.call()