javascript - jQuery .text() on multiple elements within the same class -
i'm attempting use .text() on multiple (unknown number of) elements on page.
consider:
<div class="myclass">element1</div> <div class="myclass">element2</div> <div class="myclass">element3</div>
and
$(document).ready(function(){ $( ".myclass" ).click(function() { var text = $('.myclass').text() alert(text) }); });
the problem is, .text()
return elements @ same time (in example: "element1element2element3"
).
i'd need return text within clicked class, example: click on element2
, returns "element2"
.text()
.
context key.
event callbacks run in context of trigger element. in other words, this
points element. instead of repeating selector, (unnecessarily wasteful in terms of performance), reference this
:
$( ".myclass" ).click(function() { var text = $(this).text(); //this === clicked element console.log(text); });
Comments
Post a Comment