user3475724
user3475724

Reputation:

How can I get content of div that is clicked

How to get the content of one clicked div on click event?

<div class="chosen">a</div>
<div class="chosen">b</div>
<div class="chosen">c</div>

jQuery

$(".chosen").click(function(){

    //???

});

Upvotes: 1

Views: 48

Answers (2)

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17944

you can do it like this:

$(".chosen").click(function(){
    var cntnt = $(this).html();
});

Note that this returns the content of the FIRST matched element.

Upvotes: 0

AmmarCSE
AmmarCSE

Reputation: 30557

If you only want text

$(".chosen").click(function(){
     var content = $(this).text();
});

If you want all contents(HTML elements and text nodes)

$(".chosen").click(function(){
     var content = $(this).contents();
});

Upvotes: 1

Related Questions