Prevent href from opening link but still execute other bind events

Use return false instead. You can see the code below working here. ​$(“a”).click(function() { return false; }); $(“.toolbar a”).click(function() { alert(“Do something”); });​ As pointed by @raina77ow with this article, using return false is the same as calling event.preventDefault() and also event.stopPropagation(). As I had troubles without return false on some codes in the past, … Read more

Can’t prevent `touchmove` from scrolling window on iOS

I recently ran into this same problem. You’ll need to pass { passive: false } when registering the touchmove event listener. e.g. document.addEventListener(‘touchmove’, function(e) { e.preventDefault(); }, { passive: false }); This is because document touch event listeners are now passive by default in Safari 11.1, which is bundled with iOS 11.3. This change is … Read more

Ctrl+S preventDefault in Chrome

As far as I can see, the secret sauce is, that Ctrl+S does NOT fire the keypress event, only the keydown event. Using jQuery.hotkeys: $(document).bind(‘keydown’, ‘ctrl+s’, function(e) { e.preventDefault(); alert(‘Ctrl+S’); return false; }); Only with jQuery: $(document).bind(‘keydown’, function(e) { if(e.ctrlKey && (e.which == 83)) { e.preventDefault(); alert(‘Ctrl+S’); return false; } }); Edit 2012.12.17 – jQuery.hotkeys … Read more

Stop form from submitting , Using Jquery

Again, AJAX is async. So the showMsg function will be called only after success response from the server.. and the form submit event will not wait until AJAX success. Move the e.preventDefault(); as first line in the click handler. $(“form”).submit(function (e) { e.preventDefault(); // this will prevent from submitting the form. … See below code, … Read more

When to use PreventDefault( ) vs Return false? [duplicate]

return false; return false; does 3 separate things when you call it: event.preventDefault() – It stops the browsers default behaviour. event.stopPropagation() – It prevents the event from propagating (or “bubbling up”) the DOM. Stops callback execution and returns immediately when called. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return … Read more

Bootstrap 3 – disable navbar collapse

After close examining, not 300k lines but there are around 3-4 CSS properties that you need to override: .navbar-collapse.collapse { display: block!important; } .navbar-nav>li, .navbar-nav { float: left !important; } .navbar-nav.navbar-right:last-child { margin-right: -15px !important; } .navbar-right { float: right!important; } And with this your menu won’t collapse. DEMO (jsfiddle) EXPLANATION The four CSS properties … Read more

event.preventDefault() vs. return false (no jQuery)

The W3C Document Object Model Events Specification in 1.3.1. Event registration interfaces states that handleEvent in the EventListener has no return value: handleEvent This method is called whenever an event occurs of the type for which the EventListener interface was registered. […] No Return Value under 1.2.4. Event Cancelation the document also states that Cancelation … Read more

React onClick and preventDefault() link refresh/redirect?

React events are actually Synthetic Events, not Native Events. As it is written here: Event delegation: React doesn’t actually attach event handlers to the nodes themselves. When React starts up, it starts listening for all events at the top level using a single event listener. When a component is mounted or unmounted, the event handlers … Read more

tech