ronit5rg's avatar

Why does jQuery doesn't get applied to the results of Laravel pagination from page 2 onwards

I have created a js file to go with my posts's like and dislike function. Also, I have used Laravel's paginate method to make 10 posts in a single page.

But, I don't know why the javascript doesn't seem to work for the posts from page 2 onwards.

UPDATE: this is my js file:

$('.option1').on('click', function (event) {
        var flagL=0;
        var flagD=0;
        event.preventDefault();

        var postIdl = $(this).data("idl");
        postIdl= postIdl.substr(1);
        var postId2 = 'dc'+postIdl;
        var postId1 = 'lc'+postIdl;

        if($('.'+postId2).hasClass('active')){
            flagD=1;}
        if($('.'+postId1).hasClass('active')){
            flagL=1;}
        $.ajax({
            method: 'POST',
            url: urlLike,
            data: {postIdl: postIdl, _token: token, flagL: flagL, flagD: flagD}
        });

        $('.'+postId2).removeClass('active');

    });

    $('.optionx').on('click', function (event) {
        var flagL=0;
        var flagD=0;
        event.preventDefault();

        var postIdd = $(this).data("idd");
        postIdd= postIdd.substr(1);
        var postId1='lc'+postIdd;
        var postId2='dc'+postIdd;

        if($('.'+postId2).hasClass('active')){
            flagD=1;}
        if($('.'+postId1).hasClass('active')){
            flagL=1;}

        $.ajax({
            method: 'POST',
            url: urlDislike,
            data: {postIdd: postIdd, _token: token, flagL: flagL, flagD: flagD}
        });

        $('.'+postId1).removeClass('active');

    });

This is my controller where pagination is used:

public function home()
    {
        $posts=Depress::orderBy('id','DESC')->paginate(10);
        return view('home')->with('posts',$posts);
    }

Any help will be appreciated.

0 likes
2 replies
ronit5rg's avatar

@tomo_pongrac I have updated the code. Please give a look.

Also, I wanted to point out that my javascript worked perfectly before I added the pagination. So, I think, somehow the javascript is conflicting with the Laravel pagination.

Please or to participate in this conversation.