I can add an act to my shortlist by clicking the "add to shortlist button". Acts can then be removed from the shortlist by clicking a cross.
However, after adding an act to the shortlist the act will only remove if the page is refreshed. I need to be able to dynamically add and remove acts from the shortlist, without the page refreshing, using the jquery prepend and remove methods.
Add to shortlist button
<a href="javascript:void(0);" data-id="{{ $act->id }}" class="profile-enquiry-btn add-to-shortlist" role="button">ADD TO SHORTLIST</a>
Remove from shortlist button
<a href="javascript:void(0);" data-id="{{ $item['id'] }}" class="removeact remove-from-shortlist">×</a>
$(".add-to-shortlist").click(function (e) {
e.preventDefault();
var ele = $(this);
$.ajax({
url: '{{ url('add-to-shortlist') }}' + '/' + ele.attr("data-id"),
method: "get",
data: {_token: '{{ csrf_token() }}'},
dataType: "json",
success: function (response) {
if(response.errors)
{
toastr["info"]("This act has already been added to your shortlist", "Error");
}
else
{
var data = response.data;
var $id;
var $name;
var $slug;
var $description;
var $genres;
var $genrenames = '';
var $startingprice;
var $members;
var $image;
var $rating;
$.each(data, function(i, data){
$id = data.id;
$name = data.name;
$slug = data.slug;
$description = data.description;
$genres = data.genres;
$startingprice = data.startingprice;
$members = data.members;
$image = data.image;
$rating = data.rating;
});
$.each($genres, function(i, $genres){
console.log($genres.name);
$genrenames += $genres.name+', ';
});
$('#myshortlist').prepend('<div class="popular-act" id="shortlist-item-'+$id+'"><div class="popular-act-img"><a href="/act/'+$slug+'"><img src="http://localhost:8000/storage/'+$image['id']+'/'+$image['file_name']+'" alt="'+$image['name']+'"></a><a href="javascript:void(0);" data-id="'+$id+'" class="removeact remove-from-shortlist">×</a></div><div class="popular-act-details"><div class="popular-act-top-section"><h3 class="popular-act-title"><a href="/act/">'+$name+'</a></h3><div class="popular-act-star-rating">'+$rating+'</div></div><div class="popular-act-description"><div class="popular-act-short-description">'+$genrenames.slice(0,-2)+'</div><p>insert description</p></div><div class="popular-act-bottom-section"><div class="popular-act-price"><div class="popular-act-price-heading">FROM</div><div class="popular-act-price-figure">£'+$startingprice+'</div></div><div class="popular-act-members"><div class="popular-act-members-heading">MEMBERS</div><div class="popular-act-members-figure">'+$members+'</div></div><div class="popular-act-view-profile-button"><a href="/act/'+$slug+'">VIEW PROFILE</a></div></div></div></div>');
toastr["success"]("Act added to shortlist", "Success");
}
}
});
});
$(".remove-from-shortlist").click(function (e) {
alert('Remove clicked');
e.preventDefault();
var ele = $(this);
$.ajax({
url: '{{ url('remove-from-shortlist') }}' + '/' + ele.attr("data-id"),
method: "get",
data: {_token: '{{ csrf_token() }}'},
dataType: "json",
success: function (response) {
if(response.errors)
{
toastr["info"]("A unknown error occured, please try again. Thank you.", "Error");
}
else
{
var data = response.data;
$('#shortlist-item-'+data).remove();
toastr["success"]("Act removed from shortlist.", "Success");
}
}
});
});