Search code examples
jquerye-commercepreventdefault

event.preventDefault() is not working


I'm trying to delete an item from my items on cart in an ecommerce website, im using jquery for that matter but nothing works... my link that i click on to lunch the jquery:

<a class="btn addpanier" href="addpanier.php?id=<?php echo $a['id'];?>">

my jquery code :

(function($){

$('.removepanier').click(function(event){ 
event.preventDefault();

$.get($(this).attr('href'),{},function(data){
    if (data.error) {
        alert(data.message);
    }else{
        if(confirm(data.message + '. Voulez vous revenir en accueil?')){
            location.href="index.php";
        }else{
            $('#panier12').empty().append(data.panier);
            $('#total12').empty().append(data.total1 + ' MAD');
            $('#total2').empty().append(data.total1 + ' MAD');
            $('#panier2').empty().append(data.panier);

        }
    }
},'json');
return false;
});
})(jQuery);

the code is working correctly but it takes me to the link written in href, which is not what i want...


Solution

  • $(document).ready(
    $('.removepanier').click(function(event){ 
    event.preventDefault();
    
    $.get($(this).attr('url'),{},function(data){
        if (data.error) {
            alert(data.message);
        }else{
            if(confirm(data.message + '. Voulez vous revenir en accueil?')){
                location.href="index.php";
            }else{
                $('#panier12').empty().append(data.panier);
                $('#total12').empty().append(data.total1 + ' MAD');
                $('#total2').empty().append(data.total1 + ' MAD');
                $('#panier2').empty().append(data.panier);
    
            }
        }
    },'json');
    return false;
    });
    )
    

    change your href attr with url in your html