2
How to add a class to the palette button I click on "Like"?
For example, I have several color palettes, but when I click on Like, all buttons get the class .red
, I wanted just what I clicked to get the class:
My HTML:
<form method="post" id="like">
<div class="form-group">
<input type="hidden" name="id" value="<?=$row->id?>" class="form-control">
</div>
<div class="form-group">
<input type="hidden" name="url" value="<?=Url::urlBase('palette/ajax/like')?>" class="form-control">
</div>
<button type="submit" name="submit" class="btn btn-light btn-sm">
<i class="fas fa-heart"></i> <?=$row->likes?>
</button>
</form>
My code ajax:
$('form#like').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: $('#like input[name=url]').val(),
data: $(this).serialize(),
dataType: 'json',
success: function (response) {
if (response.success) {
$('#like button[name=submit]').addClass('red')
}
}
});
});
And taking advantage of the topic, I would also like to know how I can keep the red class active even if it gives Reload on the page? Since I am not using a login system, here everyone who enters the site can give the Ikes. In the future I will do to excuse.
What’s wrong with the question?
– Max Fly