event.preventDefault() doesn't work on click


Dai Du

I want to stop event click.

I tried using event.preventDefault()"ul li a" to stop the event.

<ul>
    <li><a href="#">Click here</a></li>
</ul>

<script type="text/javascript">
$(document).ready(function() {

    $(document).on('click','ul li a', function(event){
        event.preventDefault();
    });
    $(document).on('click','ul li a', function(event){
        alert('Thank you for clicking');
    });
});
</script>

But when I click "ul li a" it shows nothing but shows alert('Thank you for clicking').

certain performance

It does successfully block the default action for that event ( redirects you to another page if done acorrectly href) . But calling doesn't block other handlers that fire the event . If you want to prevent future handlers from running when attached to the same element ( , here), use :preventDefaultdocumentstopImmediatePropagation

$(document).on('click', 'ul li a', function(event) {
  event.preventDefault();
  event.stopImmediatePropagation();
});
$(document).on('click', 'ul li a', function(event) {
  alert('Thank you for clicking');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li><a href="#">Click here</a></li>
</ul>

If the handler you want to prevent is attached to the parent element, stopPropagationuse instead. If the handler you want to block is attached to a child element, use in the capture phase stopPropagation and have the initial handler run:

ul.addEventListener('click', (e) => {
  if (!e.target.matches('ul li a')) {
    return;
  }
  e.preventDefault();
  e.stopPropagation();
}, true);

$(document).on('click', 'li a', function(event) {
  alert('Thank you for clicking');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="ul">
  <li><a href="#">Click here</a></li>
</ul>

If it awill always be with href, #it 's preventDefaultnot needed because the page won't change anyway.

Related


event.preventDefault() doesn't work on click

Dai Du I want to stop event click. I tried using event.preventDefault()"ul li a" to stop the event. <ul> <li><a href="#">Click here</a></li> </ul> <script type="text/javascript"> $(document).ready(function() { $(document).on('click','ul li a', functi

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

jQuery .click() event.preventDefault() doesn't work

Richard King I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortun

Deleting event.preventDefault on click doesn't work

Yus I have this code that prevents the default behavior for all elements: $('body *').click(function(e){ e.stopPropagation(); e.preventDefault(); }); Now, I want to programmatically click a link in the page, but first I have to remove the e.preventDefault();

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

Deleting event.preventDefault on click doesn't work

Yus I have this code that prevents the default behavior for all elements: $('body *').click(function(e){ e.stopPropagation(); e.preventDefault(); }); Now, I want to programmatically click a link in the page, but first I have to remove the e.preventDefault();

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

Bootstrap tab click preventDefault doesn't work

Stacey I'm using bootstrap 3.3 and following the tabs in the documentation. The simplest code given in the documentation doesn't work for me and I can't figure out why. $('#mainTabs a').click(function (e) { e.preventDefault() console.lo

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

event.preventdefault doesn't work in Firefox

in Nakyo I'm trying to make part of the text in the Input read-only to achieve what I found this code but doesn't work in FIrefox, works fine in chrome. Found out that event.preventDefault was causing the problem. Tried returning false, that didn't work either

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

React doesn't work for preventDefault() on onCopy event

Sviat Kuzhelev I'm trying to figure out how to make the clipboard event return falseon the onCopy event . I use test onCopyhandlers and e.preventDefault()methods. But the text is copied to the buffer without a hitch! What am I missing? Thank you in advance. im

event.preventDefault doesn't work in React

Joshua Rajandiran I followed a tutorial and they used them on event.preventDefault()a Savebutton and saved the form to the state. I haven't really written inputthe label yet, but so far I've added a "save" button, which is kind of like reloading the page when

event.preventdefault doesn't work in Firefox

in Nakyo I'm trying to make part of the text in the Input read-only to achieve what I found this code but doesn't work in FIrefox, works fine in chrome. Found out that event.preventDefault was causing the problem. Tried returning false, that didn't work either

event.preventdefault doesn't work in Firefox

Fisolotian I am using the jQuery below to toggle the hidden div. It works everywhere except Firefox, I know the error is (ReferenceError: event not defined), but I'm not sure where to define the event, so if someone could help that would be great. Thanks in ad

preventDefault() doesn't work on change event

SlimJim Any ideas why preventDefault doesn't work? Here is the code below. . . Tks! <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> $(document).ready(function() { $("#text1").change(function(e) {

.unbind() doesn't work with event.preventDefault()

Justin Medas I have a responsive Javascript file that works successfully to unbind basic jQuery functionality. When the browser is set to tablet, the navigation is toggled based on click, so I disabled linking to any li element with ul. $('.main-navigation .me

event.preventDefault doesn't work

Jose Sala I have this code below but can't get event.preventDefault() to work. The problem is, I already have other forms and it works fine. I'm sure you can help me. Here is the code: $(document).on('dblclick','td.selecionaCodigoCliente', function(){

event.preventDefault doesn't work in Firefox

username I'm not sure why, but for some reason phone calls event.preventDefault()in Firefox are not working. I use it to prevent clicking on the link that redirects ayou to href, and instead run the click function. marked as follows: jQuery: $('.sub-list-click

event.preventDefault() doesn't work on mozilla

Emi I'm having trouble getting a feature to work when using Google Chrome, but in Mozilla I'm having trouble: The JavaScript code is: <script type="text/javascript"> function gid(id) { return document.getElementById(id); } function valida

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

React doesn't work for preventDefault() on onCopy event

Sviat Kuzhelev I'm trying to figure out how to make the clipboard event return falseon the onCopy event . I use test onCopyhandlers and e.preventDefault()methods. But the text is copied to the buffer without a hitch! What am I missing? Thank you in advance. im

event.preventdefault doesn't work in Firefox

in Nakyo I'm trying to make part of the text in the Input read-only to achieve what I found this code but doesn't work in FIrefox, works fine in chrome. Found out that event.preventDefault was causing the problem. Tried returning false, that didn't work either