Try this:
$(".wrapper .button").click(function (event) {
event.preventDefault();
$(this).parent().append('<span> This content will be added </span>');
});
FIDDLE DEMO
Using your code:
$('.wrapper').append('<span> This content will be added </span>');
Append the new content to all the div with .wrapper
class. We need to firstly get the button in the current scope of the click event using this
. After, that we will get its parent using $(this).parent()
and then append the new content only to that specific div.
UPDATE
A much better code would be using the closest()
here:
$(".wrapper .button").click(function (event) {
event.preventDefault();
$(this).closest('.wrapper').append('<span>This content will be added</span>');
});
Now, closest('.wrapper')
will get the first element that matches the .wrapper selector by testing the element itself and traversing up through its ancestors in the DOM tree and making sure non-immediate descendants are not selected.