I'm surprised this error is occurring but I do not actually know how to fix it. To summarise, I'm getting this error:
ReferenceError: Can't find variable: e
But the event object should be found, as it is being passed into the function... So why is it not being found? I assume I'm doing something pretty daft here.
a {
font-size: 2em;
}
a:after {
content: 'a';
}
div.show a:after {
content: 'b';
color: blue;
}
<div class='test'>
<a onclick='testToggle(e)'></a>
</div>
<script>
const el = document.querySelector('.test');
const testToggle = (e) => {
e.preventDefault();
el.classList.toggle('show');
}
</script>
I can of course just remove the preventDefault
and e
variable, but I need the preventDefault
behaviour to stop the dom from scrolling after clicking the link.
Can someone advise where I'm going wrong here?