Genadinik
Genadinik

Reputation: 18649

How to make a jQuery and AJAX call from a url instead of a form

I know how to make a jQuery AJAX call from a form, but how do I make it from a link?

I have a list of items on a page and a link to delete each next to the item. How can I make the jQuery AJAX call from the link?

Thanks!

Upvotes: 1

Views: 60

Answers (1)

BZink
BZink

Reputation: 7967

Bind to the click handler of the link. Use an attribute of the link to hold your data or use a data-val attribute.

<a class="myLink" href="item1">item 1</a>
<a class="myLink" href="item2">item 2</a>


$(".myLink").click(function(event){
    var item = $(this).attr("href").val();
    $.ajax({
        url:"/delete/" + item,
        type:"POST",
        success: function(data){}
     });


});

Upvotes: 2

Related Questions