Reputation: 163
I'm currently trying to load a web page where complementary data is loaded through ajax when clicking on a "read more" button, but when the script is finished the page reloads. Any tip on how to prevent the page from reloading?
I tried event.preventDefault
and return false;
but that doesn't seem to work.
Here's my code:
window.setInterval(function () {
$('div.getmore').trigger('click');
return false;
}
, 1000);
Upvotes: 1
Views: 2070
Reputation: 273
You should preventDefault
click on an a
element. Not the div
if it's inside the a
:
<a href="google.com">
<div class="getmore">Get more</div>
</a>
$('a').on('click', function(event) {
event.preventDefault();
});
// Your code
Upvotes: 0
Reputation: 1173
The issue is not in your jQuery code that you pasted. Try using firebug console and click persist to make sure you see the javascript error even after page is loaded, you will then be able to find the real issue.
Upvotes: 1
Reputation: 1736
Try this , this should work
window.setInterval(function (e) {
$('div.getmore').trigger('click');
e.preventDefault()
e.stopPropagation()
}
, 1000);
Upvotes: 0