I’ve seen similar questions asked here many times but this one’s a bit different.
I have a post query displays content as;
<div class="block1">
<a class="button" href="#">Button</a>
<div class="wholiked">content</div>
</div>
<div class="block2">
<a class="button" href="#">Button</a>
<div class="wholiked">content</div>
</div>
I was trying to reload the .wholiked in .block1 upon the click of button in .block1 without refreshing the page.
But in this case all the .wholiked content inside all blocks reload instead of that specific one.
$(function() {
$('.button').click(function(evt) {
$('.wholiked').load('index.php .wholiked');
evt.preventDefault();
})
});
How can I limit it to act only inside that specific block that the button resides?
Your selector ‘.wholiked’ is loading all elements with that class. Assuming you want the one right next to the button clicked, use:
If you can’t guarantee the siblign relationship, you need to define a unique relationship somehow. One example would be to use a common parent class on the parent div like so:
The JS would then be:
Or you could put a data attribute on the button to target the element:
JS: