Ad

Get Last Activated Data After Clicked On Button By Jquery

- 1 answer

I am trying to get the last data when clicked on button in a week but nothing good show up. I have a pagination bar on this format < 1 2 3 4... > and i need to get the data when user clicked on button in order to make server processing in ajax like this:

  • Click on button {data number} it will be: page = {data number} and ajax must be: /getData/{page}

  • Clicked on button 2 ajax will send: /getData/2

  • Clicked on button 4 ajax will send: /getData/4

…but the jquery always return the activated data which is before I clicked on button, example: when I click on 2 and after that click on 4, jquery will alert I clicked on 2 and in next click on another button will alert 4. So it will get the last activated data BEFORE the activated class change to another button. This is code I used to get data from:

  var page = 1;
  function onActive(){
            page = $('.paginate_button.active>a').text();
            alert($('.paginate_button.active>a').data('dt-idx'));
        }

        $(document).on('click', '.paginate_button', function(e){
            e.preventDefault();
            onActive();
        });

The element in html like this:

<li class="paginate_button active"><a tabindex="0" data-dt-idx="3" aria-controls="staff" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#">3</a></li>

Thanks.

Ad

Answer

Try this instead.

  var page = 1;
  function onActive(_cur){
            page = cur.text();//You should get the correct page number            
         //Do whatever you want with the page
        }

    $(document).on('click', '.paginate_button', function(e){
        e.preventDefault();
        onActive($(this));
    });
Ad
source: stackoverflow.com
Ad