This may be a very dumb question, but I'm desperate right now. I'm trying to execute a javascript function by clicking on an anchor like this:
<head>
<script type=text/javascript>
function myFunction() {
alert('It was clicked');
}
Anchor.addEventListener('click', myFunction, false);
</script>
</head>
…
<a href=# id=Anchor>Click me</a>
But it's not working.
However, this works fine:
<a href=javascript:myFunction() id=Anchor>Click me</a>
And even this does:
<a href=# onclick=myFunction() id=Anchor>Click me</a>
What is the correct way to go? What am I doing wrong? A jsfiddle for example here.
Extra info:
I have set up other events using the same method, but somehow the anchor is the only one that doesn't work. Example:
<script type=text/javascript>
function aFunction() {
AnElement.style.display = 'none';
}
window.addEventListener('load', aFunction, false);
</script>
And the element is hidden perfectly fine when the window has finished loading.