1

I'm having this problem I have in jquery that when window width is bigger than 800px remove this div:

if ($(window).width() > 800) {
  $('.menu').remove();
}

But it doesn't happen instantly, you have to refresh site when window width is bigger than 800px. Btw. I have some CSS attached to .menu. I heard something about event preventDefault(), but it works with links or something like that. Can you help me please ?

SenTisso
  • 579
  • 7
  • 18
  • 1
    Consider using a CSS media query for that instead of jQuery. – Racil Hilan Mar 31 '18 at 17:55
  • downvote war between answers LOL. – Mr.ZZ Mar 31 '18 at 18:03
  • @Mr.ZZ No, it wasn't, as I didn't downvote anything here – Asons Mar 31 '18 at 18:05
  • @Mr.ZZ no, it's not. I've never downvoted an answer, and included the jQuery with attribute to LGSon, so why would I downvote an answer that I referenced? But some people downvote all answers if they don't like the question (perhaps because MikeMcCaughan marked it as a duplicate). – Racil Hilan Mar 31 '18 at 18:06
  • @LGSon Why did you delete your answer? It was the direct answer to the question. Mine is more a recommendation for an alternative solution. – Racil Hilan Mar 31 '18 at 18:09
  • I saw edit war and then downvote war xD. – Mr.ZZ Mar 31 '18 at 18:10
  • @RacilHilan Your answer had the same solution mine had, and no point having 2. – Asons Mar 31 '18 at 18:14
  • @Mr.ZZ Not everything that shine is gold :). I don't know about LGSon, but yes I edited my answer a few times. This is something I often do because you have a 5 minutes window to edit your answer without counting it as an edit. So nothing special here and certainly not a voting war. Somebody downvoted all of us (the question and answers). – Racil Hilan Mar 31 '18 at 18:17
  • @LGSon I didn't mean for my answer to have the same solution, which is why I referenced yours. I only needed it to say the last sentence, the reason why I recommended a media query. Now it is referencing a deleted answer :(. – Racil Hilan Mar 31 '18 at 18:22
  • Btw. thank you guys for your answers :D – SenTisso Mar 31 '18 at 18:22
  • @RacilHilan We likely wrote it at the same time...and now I fixed the reference issue :) – Asons Mar 31 '18 at 18:29

1 Answers1

2

Consider using a CSS media query for that instead of jQuery:

@media (min-width: 800px) {
    .menu { display: none; }
}

If you want to use jQuery (or JavaScript), you need to place that code in the resize event handler:

$( window ).resize(function() {
  if ($(window).width() > 800) {
    $('.menu').remove();
  }
});

The resize event doesn't work smoothly on some browsers especially the mobile browsers.

Asons
  • 84,923
  • 12
  • 110
  • 165
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55