javascript - Preventdefault not working -
javascript - Preventdefault not working -
hey guys new javascript web development.i have been through preventdefault()
through code.but when used it returns error ..my code
<html> <body> function preventdef(event) { event.preventdefault(); } document.queryselector('a').addeventlistener("click", preventdef(event),false); </script> <a href="www.google.com">click here</a> </body> </html>
when utilize code , clicked on link redirects me google.com ..what need event must blocked preventdefault()
function..
hope guys can help me out ..thanks
you calling preventdef
function instead of passing reference.
document.queryselector('a').addeventlistener("click", preventdef, false); // ^^^ don't phone call function
edit: issue running before dom ready. need move <script>
tag downwards after <a>
.
<html> <body> <a href="www.google.com">click here</a> <script> // ^^ did miss opening script? function preventdef(event) { event.preventdefault(); } document.queryselector('a').addeventlistener("click", preventdef, false); // ^^^ don't phone call function </script> </body> </html>
javascript html
Comments
Post a Comment