Reputation: 63
I am trying to append content to a specific div only without effecting the other divs.
How to append content to one div only without showing the content in other divs? This is a live demo of my code: http://jsfiddle.net/aGC5s/
This is jquery code:
$(".wrapper .button").click(function(event) {
event.preventDefault();
$('.wrapper').append('<span> This content will be added </span>');
});
Upvotes: 1
Views: 1748
Reputation: 56
Here my solution :
$(".wrapper").on("click", ".button", function(event) {
event.preventDefault();
$(event.delegateTarget).append('<span> This content will be added </span>');
});
Prefer to delegate event on your wrapper node, especially you want to edit it !
Also keep in mind to call off to free memory.
By using bind $(this)
refers to the delegated node (thanks to Jack). This solution is better because there is no need to search in the DOM.
$(".wrapper").bind("click", ".button", function(event) {
event.preventDefault();
$(this).append('<span> This content will be added </span>');
});
Upvotes: 1
Reputation: 73966
Try this:
$(".wrapper .button").click(function (event) {
event.preventDefault();
$(this).parent().append('<span> This content will be added </span>');
});
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.
Upvotes: 2
Reputation: 30993
Code:
$(this).closest('.wrapper').append('<span> This content will be added </span>');
Working fiddle: http://jsfiddle.net/aGC5s/10/
Upvotes: 1
Reputation: 148180
You can use $(this)
to get the source element and use closest()
to get the parent with class wrapper
of source of event.
$(".wrapper .button").click(function (event) {
event.preventDefault();
$(this).closest('.wrapper').append('<span> This content will be added </span>');
});
Upvotes: 2
Reputation: 146249
I think you want this
$(this).closest('div.wrapper').append('<span>This content will be added</span>');
Or, maybe this one
$(this).parent().append('<span>This content will be added</span>');
Upvotes: 0
Reputation: 74420
$(".wrapper .button").click(function (event) {
event.preventDefault();
$(this).closest('.wrapper').append('<span> This content will be added </span>');
});
Upvotes: 1
Reputation: 44740
$(this).closest('.wrapper').append('<span>This content will be added</span>');
Demo -->
http://jsfiddle.net/aGC5s/6/
Upvotes: 1