How to call AJAX function in anchor tag

I am new to AJAX. I want to call the AJAX function when I click the button using the onclick () event. This is an AJAX function called by the 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 fire the onclick () event

<a href="" onclick=onclickFunction(id)
+4
source share
2 answers

Just return using return inside onclick, which will prevent a reboot.

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

Alternatively you can use

<a href="javascript:onclickFunction('id')">a</a>
+3
source
<a href="#" onclick="onclickFunction('theid');">Click Me</a>

Run the function and pass it the value of 'theid'

In your example, you do not have the href add-on, and even if it was correct, you may not be able to get an identifier to pass it on. if i do this:

Click Me

: id, - , , , .

0

Source: https://habr.com/ru/post/1627579/


All Articles