pat27
pat27

Reputation: 39

How to execute Ajax code when I click on the link?

I can't figure out where the problem is :

here is the script :

<script>
$(".icross").click(function(e){
    e.preventDefault();

    var obj = $(this);

    $.ajax({
     type: "GET",
     url: "supprimer.php",
     data: 'id=' + obj.attr('rel')



success: function(html){  

},

});
</script>

and here is the html code associated :

<a href="#" class="icross" title="Supprimer" rel="80"><i class="fa fa-times"></i></a>

I want ajax to execute supprimer.php?id=80 when I click on the link but it doesn't work.

Upvotes: 0

Views: 1294

Answers (1)

MrCode
MrCode

Reputation: 64526

You're missing a comma after this line:

data: 'id=' + obj.attr('rel'),
                             ^

Also, if you don't have your script tag appearing after the element then you need a DOM ready wrapper:

$(function(){
    // code here
});

Side note: unless you have good reason to, I recommend using a data object instead of concatenating like that, because by passing an object, jQuery will handle the URL encoding for you.

data: { id: obj.attr('rel') },

Upvotes: 3

Related Questions