bigdaveygeorge
bigdaveygeorge

Reputation: 999

jQuery on click not working FireFox - possibly event.preventdefault

I have the following which is working on every browser other than Firefox. This appears to be a problem the prevent default action, how can I fix this?

$(".subcontent .sidebar .mobileopen").on("click", function () {
    event.preventDefault();
    $('.subcontent .sidebar .mobileopen').toggleClass('removeborder');
    $('.subcontent .sidebar nav.mobile').slideToggle();
    $('.subcontent .sidebar nav.mobile ul li.menu-item-has-children > a').on("click", function () {
        event.preventDefault();
        $(this).nextAll('ul').eq(0).slideToggle('slow');
        $(this).parent().toggleClass("open");
    });
});

Upvotes: 1

Views: 7993

Answers (2)

Daniel K.
Daniel K.

Reputation: 11

This one worked for me

$("body").on("click", ".subcontent .sidebar .mobileopen" function (event) {
    event.preventDefault();

});

Upvotes: 0

Tushar
Tushar

Reputation: 87233

You haven't passed the event parameter to the event handler

$(".subcontent .sidebar .mobileopen").on("click", function (event) {
                                                            ^^^^^
    event.preventDefault();

Mozilla Firefox don't have global event. Check https://stackoverflow.com/a/33167145/2025923.

You can also use return false; at the end of the event handler to prevent default action of the element from happening. This will also stop event bubbling.

Upvotes: 5

Related Questions