Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call AJAX function in anchor tag

I'm new to AJAX. i want to call AJAX function when i click a button using onclick() event. This is the AJAX function called by onclick() event

function onclickFunction(aId){
    $.ajax({
        type: "get",
        url: "like_audio",
        data: {
            aId:aId
        },
        success: function (data){
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError){

        }
    });
    return false;
}

This is how i'm calling the onclick() event

<a href="" onclick=onclickFunction(id)
like image 340
Nibras Avatar asked Feb 06 '16 16:02

Nibras


1 Answers

Just get the use return inside onclick, which will prevent the reload.

<a href="" onclick="return onclickFunction('id')">a</a>

As an alternative you can use

<a href="javascript:onclickFunction('id')">a</a>
like image 161
rrk Avatar answered Oct 18 '22 19:10

rrk