owf
owf

Reputation: 251

jQuery selector to capture class inside php if statement

I'd like to make a selector to capture click events on code below :

index.php :

<ul class="pagination">
  <?php

    if ($page > 1) {
      $link = $page - 1;
      $prev = '<li class="page-item"><a class="page-link" href="javascript:void(0);" id="'.$link.','.$record_per_page.'">Previous</a></li>';
    }else{
      $prev = '<li class="page-item"><a class="page-link" href="javascript:void(0);">Previous</a></li>';
    }

    $num = '';

    for($i = 1; $i<=$total_pages ; $i++){
      if($i == $page){
        $num .= '<li class="page-item"><a class="page-link" href="javascript:void(0);" id="'.$i.'">'.$i.'</a></li>';
      }else{
        $num .= '<li class="page-item"><a class="page-link" href="javascript:void(0);" id="'.$i.','.$record_per_page.'">'.$i.'</a></li>';
      }
    }

    if ($page < $total_pages) {
      $link = $page + 1;
      $next = '<li class="page-item"><a class="page-link" href="javascript:void(0);" id="'.$link.','.$record_per_page.'">Next</a></li>';
    }else{
      $next = '<li class="page-item"><a class="page-link" href="javascript:void(0);">Next</a></li>';
    }

    echo $prev.$num.$next;

  ?>
</ul>

script.js :

$(document).on('click','.page-link',function () {
console.log('aaaaaa');
});

I already tried codes above, but it doesn't work, the console.log never showed up, is there any problem with my php code or jquery code ?

Upvotes: 1

Views: 129

Answers (1)

owf
owf

Reputation: 251

Thanks to all , and especially for Ritesh Khandekar, who gave me this code to solve my problem :

$(document).on('click','ul li .page-item',function() { console.log('aaaaaa'); });

I don't know why using page-item instead of page-link, but it workssss !!!!

Upvotes: 2

Related Questions