1

I do have the following click function on a div element. Inside the div element are links. How can I stop the click function on this links? I tried z-index but it doesn't work.

$('.thumb.flip').click(function () {
 if ($(this).find('.thumb-wrapper').hasClass('flipIt')) {
     $(this).find('.thumb-wrapper').removeClass('flipIt');
 } else {
     $(this).find('.thumb-wrapper').addClass('flipIt');
 }
});
  • This has already been answered: http://stackoverflow.com/questions/5085584/disable-a-hyperlink-using-jquery – Niklas Jan 31 '14 at 15:13

7 Answers7

1

Try e.preventDefault()

$('.thumb.flip').click(function (e) {
    e.preventDefault();
    if ($(this).find('.thumb-wrapper').hasClass('flipIt')) {
        $(this).find('.thumb-wrapper').removeClass('flipIt');
    } else {
        $(this).find('.thumb-wrapper').addClass('flipIt');
    }
});
Anton
  • 32,245
  • 5
  • 44
  • 54
1

Add code to stop the click from propagating from the links up the DOM to the div:

$('a').click(function(e){e.stopPropagation();})

See http://api.jquery.com/event.stoppropagation/

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
1

Capture the event object:

$('.thumb.flip').click(function (evt) {

Test to see what sort of element was clicked:

if (evt.target.tagName.toLowerCase() === "a") {
    return true;
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Use the mouse down event and in your handler call event.stopPropagation() and event.preventDefault();

S..
  • 5,511
  • 2
  • 36
  • 43
0

if click target is an a just return false else do normal stuff...

$('.thumb.flip').click(function (e) {
    if ($(e.target).is('a')) {
        return false;
    }
});

z-index won't do anything, however if you want to prevent clicks from css you can add pointer-events property to links and set the value none. That will disable all kind of events on links.

Ehtesham
  • 2,967
  • 1
  • 18
  • 20
0
$('.thumb.flip').click(function (e) {
    if (e.target.nodeName==="A") {  //if target is an anchor, ignore
        return;
    }
    $(this).find('.thumb-wrapper').toggleClass('flipIt')  //toggle class
});
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

If you can edit markup do this:

<a href="javascript:void(0);" ></a>
Eugene P.
  • 2,645
  • 19
  • 23