Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Follow link inside JQuery click event

Tags:

jquery

I have links inside <td>s, but I also have a click event on all <td>s. The code looks like this:

$( document ).ready( function() {
    $( 'td.event' ).click( function() {
        var eventName = prompt( 'Enter event:' );
        if ( eventName != null && eventName.length > 0 ) {
            window.location = '?event=' + eventName;
        }
    } );
} );

I want to simply follow the link without showing the popup if the user clicks the link, but to show the popup if the user clicks anywhere else in the <td>. Is this possible in JQuery?

like image 552
Sean Kelleher Avatar asked Feb 24 '12 22:02

Sean Kelleher


2 Answers

Add this to the end of your document.ready handler:

$( 'td.event a' ).click( function(e) {
    e.stopPropagation();
} );
like image 64
bfavaretto Avatar answered Nov 15 '22 03:11

bfavaretto


function yourFunction()
{
   var eventName = prompt( 'Enter event:' );

   if(eventName != null && eventName.length > 0) {
      window.location = '?event='+eventName;
   }
}

$('td').click(function() {
   yourFunction();
});

$('td a').click(function() {
   window.location = $(this).attr('href');
});

should do the trick.

like image 35
Grigor Avatar answered Nov 15 '22 03:11

Grigor