Have a div#ajax-loader somewhere in your document and set it's styles:
/* You can style this further - this is just an example */ #ajax-loader { position: absolute; width: 100%; height: 100%; text-align: center; color: #fff; background: rgba(0, 0, 0, 0.5); padding-top: 35%; display: none; }
And place it in your document as:
<div id="ajax-loader">Loading content...</div>
Now, change your jQuery code into:
$("#send").click(function() { if ($("#ccval").val() == $("#contactcaptcha").text()) { var id = $("#contactcaptcha").attr("data-id"); var cemail = $("#cemail").val(); // notice this line $('#ajax-loader').show(); // this will set display: none; -> display: block; for this element $.post(base_url + "index.php/myad/getphonenumber", { uniqueid: id, emailaddress: cemail }, function() { alert('email is sent'); // notice this line too $('#ajax-loader').hide(); // just toggle the view vice-versa... }) } else { $("#ccval").val(""); $("#ccval").attr("placeholder", "invalid captcha"); } });
EDIT: Also, an answer below posted by Kev is highly suggested.
.always();