dusty
dusty

Reputation: 41

jquery select div inside div

I am trying to get this to alert only the div clicked on, vs alerting the div clicked and then the container div's. when I click on the third div I would like it to only alert "third". thanks for the help.

<div id="first">   
    <div id="second">
        <div id="third">
            third div
        </div>
        second div
    </div>
    first div
</div>

$("div").click(function() {
    alert(this.id);
});

Upvotes: 4

Views: 543

Answers (4)

Dmitry
Dmitry

Reputation: 902

$("div").click(function() {
    alert(this.id);
    return false;
})

Upvotes: 2

Facundo Farias
Facundo Farias

Reputation: 408

$("div").click(function(event) {
    alert(this.id);
    event.stopPropagation();
})

Upvotes: 2

Rusty Fausak
Rusty Fausak

Reputation: 7525

event.stopPropagation();

$("div").click(function(event) {
    alert(this.id);
    event.stopPropagation();
})

Upvotes: 2

Joseph Marikle
Joseph Marikle

Reputation: 78570

Demo

$("div").click(function(e) {
    alert(this.id);
    e.stopPropagation();
})

that will do it.

Upvotes: 4

Related Questions