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. Unfortunately I can't pass .preventDefault()

Here is my link format:

<a href="path/to/file.php" class="filelink">Some Text</a>

Here's the jQuery I'm trying to use to stop the default action:

    $( '.filelink' ).click( function( e ) {
        e.preventDefault();
        //do some other stuff here
    });

edit:

More information:

This <script>...</script>should be my footer. This function follows $(document).readya $.ajaxcall that builds a list of links that this function tries to listen for clicks.

r3wt:

Since your link is dynamically attached to the page, you need to use it document.on()to capture click events.

The syntax for attaching an event listener to dynamic content is as follows

$(document).on( event, selector, callback );

So your click handler would become:

$(document).on('click','.filelink',function(e){

 e.preventDefault();

 //code here

 return false;

});

Related


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

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

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

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

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();

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 event.preventDefault() doesn't work unless debugging

metal encoder I am trying to use this method in order to convert GET parameters to POST. I also want the anchors to be handled correctly even when the middle mouse button is clicked. This is how the event handler is bound: $(document).ready(function () { $

jQuery event.PreventDefault doesn't work in user defined function

Mickey I am creating a function that will receive a URL and then display that URL in a lightbox. I'm having trouble with event.preventDefault() in a function, it says it's not a function in the error console. I've tried passing the event to the function explic

jQuery event.PreventDefault doesn't work in user defined function

Mickey I am creating a function that will receive a URL and then display that URL in a lightbox. I'm having trouble with event.preventDefault() in a function, it says it's not a function in the error console. I've tried passing the event to the function explic

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

jQuery .off("click") event doesn't work

basic need help This has been bugging me for hours and still confused as to why it doesn't work... When I use table.off("click");it , it also unbinds the click event of the first child without id=multi. var table = $("table#datatable") var button = $(".generat

jQuery click event doesn't work

mightyspaj3 I have this simple jQuery click event and for some reason it's not working. works, I can use this inside the $(document).ready(function() {});alert; however the actual click event doesn't work. Thanks in advance and please take it easy on me as I d

MathJax doesn't work in Jquery .click event

Glagas Incoming MathJaxJquery .clickevent doesn't work inside but outside that function. Run the code snippet to see the effect. $('#math').click(function(e){ e.preventDefault(); $("#result").html("$$ MathJax inside .click event doesn't work $$"); }); //outsid

jQuery Click event doesn't work on mobile

lkw3274 I'm trying to make the following JSFiddle work for tablet/mobile (eg "touch" and "click"). https://jsfiddle.net/lkw274/7zt1zL0g/87/ <div class="user-navigation"> <a class="mobile-menu-new" href=""><span></span>Menu</a> </div> $(document).ready

click event on jQuery doesn't work

Creep to death I have printed the echo with php of the input element and I need to get the click event id using jquery. Below is the code I am trying to get API.php (Echo elements to index.php using ajax): echo"<input type='submit' id='something' value='accept

JQuery click() event listener doesn't work

it consolidates I'm trying to get a small project going, but I'm stuck at a very annoying thing. $(document).ready(function() { $("#search-button").click(console.log('hello')) }); As you can see, I'm targeting a search button using the id, and search-buttona

jQuery .off("click") event doesn't work

basic need help This has been bugging me for hours and still confused as to why it doesn't work... When I use table.off("click");it , it also unbinds the click event of the first child without id=multi. var table = $("table#datatable") var button = $(".generat

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

click event on jQuery doesn't work

Creep to death I have printed the echo with php of the input element and I need to get the click event id using jquery. Below is the code I am trying to get API.php (reflects elements to index.php using ajax): echo"<input type='submit' id='something' value='ac