I'm creating a follow/unfollw buttons for different members in a system. I'm having an issue with jQuery event not being fired after html is rendered again in the same div where the buttons are. The follow/unfollow-buttons works FIRST time - always, but never on second try. Other things that are depending on javascript still works so it's not an error in js either.
//html snippet
<div id="start-members">
<div class="content">
<div class="result">
<div class="items-column member"><div class="image-holder">
<img src="imagefile"></div>
<span class="button-section">
<a href="#" data-userid="61" class="follow button trans-effect follow-user"><span class="following-text">Follow</span><span class="unfollow-text">Unfollow</span></a>
</span>
<div class="clearfix"></div></div>
<div class="items-column member"><div class="image-holder">
<img src="imagefile"></div>
<span class="button-section">
<a href="#" data-userid="95" class="follow button trans-effect follow-user"><span class="following-text">Follow</span><span class="unfollow-text">Unfollow</span></a>
</span>
<div class="clearfix"></div></div>
<div class="items-column member"><div class="image-holder">
<img src="imagefile"></div>
<span class="button-section">
<a href="#" data-userid="104" class="follow button trans-effect follow-user"><span class="following-text">Follow</span><span class="unfollow-text">Unfollow</span></a>
</span>
<div class="clearfix"></div></div>
</div>
</div>
</div>
//jQuery
//This returns html and put it into result-div above
function getLatestMembers() {
var getLatest = $.ajax({
type: 'GET',
url: '/index.php/members/getlatest',
dataType: 'json'
});
//If success, returning html
getLatest.done(function(data) {
$("#start-members .result").html(data);
});
getLatest.fail(function(ts) {
alert(ts.responseText);
});
}
//Toggle between following this user or not
$(".content .button-section").on('click', '.button.follow-user', function(e) {
e.preventDefault();
//doing toggle of the button (to unfollow-user/follow-user)
getLatestMembers(); //When having this call it renders the div latest members and next time this event is not fired. why?
});
//Unfollow user that has created this outfit
$(".content .button-section").on('click', '.button.unfollow-user', function(e) {
e.preventDefault();
//doing toggle of the button (to unfollow-user/follow-user)
getLatestMembers(); //When having this call it renders the div latest members and next time this event is not fired. why?
});
I've also tried just having getLatestMembers()
in the event call. Next time the event is not fired.
//Unfollow user that has created this outfit - fired only once
$(".content .button-section").on('click', '.button.unfollow-user', function(e) {
e.preventDefault();
getLatestMembers();
});
I hope I've described the scenario so you guys understand my problem/issue:-) (Question is: Why is not event fired more then once?)