Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

pagination using ajax in codeigniter

i want to create a pagination using ajax but i am getting trouble for it. i use folliwing ajax function for that.

<script type="text/javascript">
 $(function(){
     $('#ajax_pagi a').click(function(e){          
       var url = $(this).attr('href');
         $.ajax({
           url:url,
           type:'POST',
           success:function(data){          
               $('body').html(data);
           }
         });
         e.preventDefault(); 
         return false;
    });
  }); 
     </script>

and following is pagination link

<ul id="ajax_pagi"> <?php echo  $this->pagination->create_links(); ?></ul>

it refresh page when click on the next page link. so any one can help me to solve this. thanks in advance.

Upvotes: 1

Views: 122

Answers (1)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

This is because of event delegation. for the first time it will work, next time it will refresh the page.

So use on() instead of click().

$(document).on("click","#ajax_pagi a",function(e){          
   var url = $(this).attr('href');
     $.ajax({
       url:url,
       type:'POST',
       success:function(data){          
           $('body').html(data);
       }
     });
     e.preventDefault(); 
     return false;
});

For current page href will be equal to #, if user click on that it will generate 404 error.

Add if() for ajax

if(url != "#") {
   //ajax here
}

Upvotes: 1

Related Questions