Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery select div inside div

Tags:

jquery

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);
});
like image 975
dusty Avatar asked Dec 27 '22 13:12

dusty


2 Answers

Demo

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

that will do it.

like image 194
Joseph Marikle Avatar answered Dec 30 '22 02:12

Joseph Marikle


event.stopPropagation();

$("div").click(function(event) {
    alert(this.id);
    event.stopPropagation();
})
like image 20
Rusty Fausak Avatar answered Dec 30 '22 03:12

Rusty Fausak