You need to re-render the element
Sep 8, 2022
10
Level 7
Like button using Axios and Laravel
Hello everyone! I have created my first like button and instead of refresh page when user like the post , i have used axios it has success responce but the button style dosn't change , how can i change it..??
<script>
document.getElementById('submit-button').addEventListener("click", function(event){
event.preventDefault()
const url = '/libraries/{{$library->id}}/like';
axios.post(url).then(function() {
var el = document.getElementsByClassName('submit-button');
// change the color style of button every time user click on it..
console.log('test')
})
});
</script>
Level 37
@mutaz well that's your controller responsibility, make some checks, if it's already liked, return a 409 status which indicates that this like already exists, so you can do something like this
axios.post(url).then(function(response) {
// this mean the library is not liked yet
if (response.status != 409) {
var el = document.getElementsByClassName('submit-button');
el.classList.add("mystyle");
}
})
2 likes
Please or to participate in this conversation.