Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I still don't know what was wrong with <code>undelegate</code> which I couldn't make it work, but I managed to fix my code by using <code>on</code> and <code>off</code>.</p> <p>As <a href="https://stackoverflow.com/questions/16266581/jquery-undelegate-does-not-work?noredirect=1#comment23277711_16266581">adeneo said</a> I was delegating and undelegating on each window resize which was quiet a bug and I think I fixed that but holding the last state on <code>device</code> variable.</p> <pre><code>var $window = $(window); var device; function desktopSidenav() { $(".nav-list &gt; li").off('click'); $(".nav-list &gt; li").on('mouseover', function(e) { $(this).find("a").addClass('active'); $(this).find("div.sub-items").toggle(); }).on('mouseout', function(e) { $(this).find('a').removeClass('active'); $(this).find("div.sub-items").toggle(); }); } function handheldSidenav() { $(".nav-list &gt; li").off('mouseover').off('mouseout'); $(".nav-list &gt; li").on('click', function(e) { if ($(this).find("div.sub-items").is(':hidden')) { $(this).find("a:first").addClass('active').next().slideDown('slow'); } else { $(this).find("a:first").removeClass('active').next().slideUp('slow'); } e.preventDefault(); }); } </code></pre> <p>Now I check the window size before doing anything else an I'll hold the device type in <code>device</code> variable. If window is resized, I'm gonna check the device state and do the things based on device type.</p> <pre><code>if ($window.width() &gt; 767) { device = 'desktop'; desktopSidenav(); } else { device = 'handheld'; handheldSidenav(); } $window.resize(function() { if ($window.width() &gt; 767) { if (device == 'handheld') { device = 'desktop'; desktopSidenav(); } } else { if (device == 'desktop') { device = 'handheld'; handheldSidenav(); } } }); </code></pre> <p>If I use <code>delegate</code> and <code>undelegate</code> instead of <code>on</code> and <code>off</code>, the code won't work and I still don't know why, so this cannot be count as a real answer, but I wanted to tell everyone who has a similar problem to use jQuery's <code>on</code> and <code>off</code> instead on <code>delegate</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload