dflow dflow - 2 months ago 16
Ajax Question

How to execute a click to call href:tel with a AJAX Post/Put?

How can I post before the default browser click to call? The default browser action stops the posting action. I do not get data in the

console.log
.

<a id="callButton" class="btn btn-lg btn-success" href="tel:+1-999-115-4490">Call Customer Support/a>


$(document).ready(function(){
$("#callButton").click(function(){

$.ajax({
type: 'POST',
url: 'foo',
data: 'ok',
success: function(data) {
console.log(data);

}
});
});
});

Answer

To achieve this you need to prevent the default event using preventDefault(). Then in the callback of your AJAX you can redirect to the href specified in the a element. Try this:

$("#callButton").click(function(e) {
    e.preventDefault(); // stop the default event
    var $a = $(this);
    $.ajax({
        type: 'POST',
        url: 'foo',
        data: 'ok',
        success: function(data) {
            window.location.assign($a.attr('href')); // redirect to the URL after the AJAX completes.
        }
    });
});
Comments