Bora
Bora

Reputation: 10717

Accessing jQuery $(this) object on plugin init

I stacked about accessing $(this) object in twbsPagination pagination plugin.

<ul class="dynamic-pagination pagination-sm" data-target="invoices"></ul>

On onPageClick option, $(this) object working perfect!

    $('.dynamic-pagination').twbsPagination({
        totalPages: 35,
        visiblePages: 7,
        onPageClick: function (event, page) {
            $($(this).attr('data-target')).text('Page ' + page);
        }
    });

But on href option, $(this) doesnt working.

    $('.dynamic-pagination').twbsPagination({
        totalPages: 35,
        visiblePages: 7,
        href: 'admin?'+$(this).attr('data-target')+'_page={{number}}'
    });

I have to access $(this) object from href option. I couldnt find the solution on web and SO.

Upvotes: 0

Views: 291

Answers (1)

Abhishek Goyal
Abhishek Goyal

Reputation: 877

You can do this as follows :

$('.dynamic-pagination').each(function () {

    var $this = $(this), dataTarget = $this.attr('data-target');

    $this.twbsPagination({

        totalPages: 35,

        visiblePages: 7,

        href: 'admin?' + dataTarget + '_page={{number}}'

    });

});

Upvotes: 1

Related Questions