Skip to content Skip to sidebar Skip to footer

How To Stay The Color Of The Button When Hit Like And Refresh Page

the default color of my like button is gray and when user hit like button it will run the togglepost 'like' and the color will change to red.. i use ajax to insert the data if the

Solution 1:

You'd have to do some Ajax to save the state in your database or some cache on the filesystem for example.

If you want something less robust, you could save the state in a cookie or localstorage. Or even less persistent: in a session variable, that will be gone once the Browser session is lost.

Then when the page is requested check against the presence of your state in database, cache, cookie, session or whatever and give the button the corresponding class in your HTML.

If you are using PHP, for example, it could be done like so:

<?php
$buttonClasses = ['coracao'];
$didLike = your_read_from_cache_function();

if ($didLike)
    $buttonClasses[] = 'ativo';
?>
<div class="boxcoracao">
    <span class="<?= implode(' ', $buttonClasses) ?>" name="like"><br>&emsp;&emsp;&emsp;Love</span>
</div>

Post a Comment for "How To Stay The Color Of The Button When Hit Like And Refresh Page"