Jio Jio - 1 month ago 19
Javascript Question

event.preventDefault -- in Firefox, without jquery

This works in Chrome:

<a href="#" onclick="myFunction()">Aaa</a>

function myFunction() {
event.preventDefault();
//.........


But in FireFox event isn't defined. How can I get it to work in Firefox without using jquery?

update:

function myFunction(event) {
event.preventDefault();
}


how will event arrive to myFunction if it's not been passed to it from onclick?

Answer

You lack a parameter there, which is the event.

function myFunction(event) {
    event.preventDefault();
    console.log("It worked");
}

Also, in your onclick attribute of the anchor tag, pass the event as argument:

<a href="#" onclick="myFunction(event)">Aaa</a>

To verify that it solved the problem, here's a runnable code snippet:

<a href="#" onclick="myFunction(event)">Aaa</a>

<script>
function myFunction(event) {
    event.preventDefault();
    console.log("It worked");
}
</script>