sami
sami

Reputation: 1362

.click() function use with ajax

JavaScript:

<script>

$('#alink').click(function () {
     $("#loading").ajaxStart(function(){
    $(this).show();
 }).ajaxStop(function(){
    $(this).hide();
 });

$('#aresult').load('content2.html', function() {
 $('#successdata').html('100% loaded');
});
 });
 </script>

HTML:

<a href="#" id="alink">click me</a>
<div id="aresult"></div>

What is the error of this code ? I want to load ajax call page when user click on the mouse with loading image

Upvotes: 1

Views: 86

Answers (1)

adeneo
adeneo

Reputation: 318212

I'd do it like this, and avoid the global ajax events:

<script type="text/javascript">
    $(function() {
        $('#alink').on('click', function () {
             $("#loading").show();
             $('#aresult').load('content2.html', function() {
                 $('#successdata').html('100% loaded');
                 $("#loading").hide();
            });
        });
    });
</script>

Also remember the document.ready function etc.

Upvotes: 5

Related Questions