CriketX CriketX - 1 year ago 296
Javascript Question

jquery .click() event.preventDefault() not working

I'm building a web application that is extendable by dropping scripts into a preset directory. The script needs to read file headers before going to a page, so I need to use .preventDefault() when links are clicked and run some JScript first. Unfortunately I can't get past the .preventDefault()

Here's 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 ) {
//do some other stuff here


More Info:

is in my footer.
This function is INSIDE
call that builds the list of links this function is trying to listen for clicks of.

Answer Source

Since your links are appended dynamically to the page, you need to use document.on() to capture the click events.

the syntax for appending event listeners to dynamic content is as follows

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

so your click handler would become:



 //code here

 return false;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download