On my website you can add cards to your collection. When you have clicked on the plus JS removes the plus button and inserts the minus button. The minus button is now visible but not working. I've read some articles about the new appended button not being clickable because the button didn't exist when the page was first loaded. Hence also when I reload the page the minus is working fine.
here's my code:
HTML
echo '<li class="card addcards" id="card_' . $card["id"] . '">';
echo '<div class="status addtocol_btn addtocol_card_'.$card['id'].'" id="'.$card['id'].'"><span id="addtocol_'.$card['id'].'">'.$col_id.'</span>+</div>';
echo '</li>';
JS
// AJAX ADD CARD TO COLLECTION ON SEARCHPAGE
$(document).ready(function(){
$(".addtocol_btn").click(function(){
var card_id = $(this).attr('id');
var col_id = document.getElementById('addtocol_'+card_id).innerHTML;
$.ajax({
url: ""+homelink+"/addtocol.php",
method: "POST",
data:{'card_id':card_id, 'col_id':col_id},
success: function(result){
$('div[id="' + card_id + '"]').fadeOut();
$('#card_'+card_id+'').append('<div class="status del_btn del_btn_card_'+card_id+'" id="'+card_id+'"><span id="removefromcol_'+card_id+'">'+col_id+'</span>-</div>');
}});
});
});
// AJAX REMOVE CARD FROM COLLECTION ON SEARCHPAGE
$(document).ready(function(){
$(".del_btn").click(function(){
var card_id = $(this).attr('id');
var col_id = document.getElementById('removefromcol_'+card_id).innerHTML;
//if (confirm("Are you sure you want to remove this card from your collection?")) {
$.ajax({
url: ""+homelink+"/delete_card.php",
method: "POST",
data:{'card_id':card_id, 'col_id':col_id},
success: function(result){
$('div[id="' + card_id + '"]').fadeOut();
$('#card_'+card_id+'').append('<div class="status addtocol_btn addtocol_card_'+card_id+'" id="'+card_id+'"><span id="addtocol_'+card_id+'">'+col_id+'</span>+</div>');
}});
//}
});
});
I've checked with the Jquery manual: http://learn.jquery.com/events/event-delegation/ and aplied the there shown code but that just breaks my entire add/remove code:
// AJAX ADD CARD TO COLLECTION ON SEARCHPAGE
//$(document).ready(function(){
$(".card").on("click", "div", function( event ){
event.preventDefault();
var card_id = $(this).attr('id');
var col_id = document.getElementById('addtocol_'+card_id).innerHTML;
$.ajax({
url: ""+homelink+"/addtocol.php",
method: "POST",
data:{'card_id':card_id, 'col_id':col_id},
success: function(result){
$('div[id="' + card_id + '"]').fadeOut();
$('#card_'+card_id+'').append('<div class="status del_btn del_btn_card_'+card_id+'" id="'+card_id+'"><span id="removefromcol_'+card_id+'">'+col_id+'</span>-</div>');
}});
});
//});
Who can help me out here? Thanks in advance.