I want to trigger a function when has an specific class, in this case Clicked
so I want to return the alert (unliked)
HTML
<div class="social">
<div class="LikePost default"></div>
</div>
JQUERY
$(".social").find(".LikePost").trigger("click"); /*when has class "clicked"*/
alert("unliked")
TRIGGERED FUNCTION
$('.LikePost').on('click', function() {
if ($(this).hasClass("default")) {
$(this).addClass("Clicked");
$(this).removeClass("default");
alert("post liked");
} else {
$(this).addClass("default");
$(this).removeClass("Clicked");
alert("post unliked");
}
});
I want to trigger a function when has an specific class, in this case Clicked
so I want to return the alert (unliked)
HTML
<div class="social">
<div class="LikePost default"></div>
</div>
JQUERY
$(".social").find(".LikePost").trigger("click"); /*when has class "clicked"*/
alert("unliked")
TRIGGERED FUNCTION
$('.LikePost').on('click', function() {
if ($(this).hasClass("default")) {
$(this).addClass("Clicked");
$(this).removeClass("default");
alert("post liked");
} else {
$(this).addClass("default");
$(this).removeClass("Clicked");
alert("post unliked");
}
});
$(".clicked").trigger("click")
?
– user1636522
Commented
Mar 20, 2018 at 20:17
Alright x-D
$(".Clicked").trigger("click")
You can rewrite your trigger like this:
$('.LikePost').on('click', function(){
// just invert classes state
$(this).toggleClass("default");
$(this).toggleClass("Clicked");
// if is default, its liked, if not, its not liked
if($(this).hasClass("default")) {
alert("post liked");
} else {
alert("post unliked");
}
});
And if you want to manually trigger all liked posts, just do it:
$('.Clicked').trigger('click');