So, i know it is not the best of practices to use <a href="javascript: foo()">
and pls don't bash me for it. It is at the moment the best solution for an issue I have.
I am editing a website with a bunch of non-interactive elements such as div
and spans
which represent menus, drop-downs, buttons and so on. And onto the stage walks accessibility in all it's glorious hell raising issues.
So to solve it:
<div class="btn" onclick="foo()"> EDIT </div>
<!--had to become-->
<a href="javascript: foo();"><div class="btn">EDIT</div>
Now this obviously raises some issues with event targeting and whatnot, but let's forget those for now, most of the functions invoked from these buttons are actually just AJAX calls for new pages and such.
The bigger issue is the following:
How does one access any reference to the <a>
tag in the foo()
call? this
is set to window
and event
is undefined
. I tried passing event
into the brackets like so:
<a href="javascript: foo(event)">
Still didn't work. So is there a way to access a reference to the <a>
tag that invoked the function within the function itself?
EDIT to clarify why it is not a duplicate:
This question has nothing to do with this
in an onclick
. It has to do with getting a reference without using the onclick
and instead using href="javascript: foo()"
which is not the same as using onclick.