event.preventDefault() vs return false: what's the difference in event handling?
Hello everyone! I've just started learning JavaScript and jQuery, and I came across a strange thing. When I write a click handler for a link, I want to prevent navigation. In some examples I see `event.preventDefault()`, and in others just `return false` at the end of the function. Do they do the same thing?
Here's an example that confuses me:
```javascript
// Option 1
document.querySelector('a').addEventListener('click', function(event) {
event.preventDefault();
console.log('Link did not work');
});
// Option 2 (with jQuery)
$('a').click(function() {
console.log('Link did not work');
return false;
});
```
Both options seem to work the same way – the link does not navigate. But I noticed that if I use `return false` in pure JavaScript (without jQuery), it doesn't
No answers yet.
Log in to leave an answer
Blogs
PHP for Machine Learning: Capabilities and Limitations in Practice
Deploying Java on Kubernetes: A Basic Guide for Beginners
Salaries of Kotlin Developers in Russia: Market Overview for Advanced Level
How to Get a Property Value by Name via Reflection in C#: A Complete Guide