Marwann
Marwann

Reputation: 163

Prevent page from reloading after JQuery triggers

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

Answers (3)

Лёша Ан
Лёша Ан

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

shivgre
shivgre

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

prabin badyakar
prabin badyakar

Reputation: 1736

Try this , this should work

window.setInterval(function (e) {
        $('div.getmore').trigger('click'); 
        e.preventDefault()
        e.stopPropagation()
       }
, 1000);

Upvotes: 0

Related Questions