Tom Miron
Tom Miron

Reputation: 57

Prevent children tags on click redirect

I have the following code

<div class="takeover" onclick=location.href="http://google.com/">
<div class="site-container">

And I want that onclick to work ONLY on Takeover, nothing inside takeover.

How do I do that?

Thanks,

Tom

Upvotes: 1

Views: 90

Answers (1)

Popnoodles
Popnoodles

Reputation: 28409

You need to check that the target is being clicked, as its children's clicks will bubble up

http://jsfiddle.net/bovuszu1/1/

<div class="takeover" onclick="javascript: if (event.target == this) location.href='http://google.com/';">

or with jQuery

http://jsfiddle.net/bovuszu1/2/

$(function(){
    $('.takeover').on('click', function(e){
        if (e.target == this) location.href='http://google.com/';
    });
});

Upvotes: 1

Related Questions