1

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");
    });
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bigdaveygeorge
  • 947
  • 2
  • 12
  • 32
  • 2
    $( ".subcontent .sidebar .mobileopen" ).on( "click", function(event) { dont know if this helps. But you need to apply the event on the function – Tommy Oct 19 '15 at 15:00
  • `event` should be a parameter to the handler function, is what @Tommy correctly suggests :) – Pointy Oct 19 '15 at 15:00
  • Take a look here: http://stackoverflow.com/questions/4585970/jquery-event-preventdefault-not-working-in-firefox-jsfiddle-included – Dalibor Oct 19 '15 at 15:03

2 Answers2

5

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.

Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

This one worked for me

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

});
Daniel K.
  • 11
  • 4