Get last activated data after clicked on button by jquery
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.
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));
});
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM