Tomislav Tomi Nikolic Tomislav Tomi Nikolic -4 years ago 105
jQuery Question

Changing class text of only one class in Jquery

Alright so I am trying to change only text of the current class not all of them.

For example

$(".post_like").click(function() {
$(".upvotes_changer_inner").text("Hello world");
});


And the HTML would be

<div class="post_like">
<p class="upvotes_changer_inner">
Change me
</p>
</div>

<div class="post_like">
<p class="upvotes_changer_inner">
Change me
</p>
</div>

<div class="post_like">
<p class="upvotes_changer_inner">
Change me
</p>
</div>


Example on Jsfiddle:
https://jsfiddle.net/09jgeb60/

The problem is basically, it should change only the element I've clicked on, not all of them

Answer Source

You need to use $(this) to refer to the element being clicked:

$(".post_like").click(function() {
    $(this).find(".upvotes_changer_inner").text("Hello world");
});

jsFiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download