jquery - addClass in $(window).resize function .on('click' not working -


please have @ jsfiddle link http://jsfiddle.net/3arqa/

if background orange means ul has mobile-navadded class name else background yellow.

now if orange , click on link should alert here, nothing happen.

if uncomment line in $(document).ready(function....it work, if add class in markup section.

what doing wrong? want class mobile-navto toggle depending on screen size , of course on.('click' working if have class ul.nav.mobile-nav...

here code, help!

<span class="show-window-width"></span>  <ul class="nav">     <li>         <a href="#">one</a>     </li>     <li>         <a href="#">two</a>     </li>     <li>         <a href="#">three</a>     </li>     <li>         <a href="#">four</a>     </li> </ul>  <script type="text/javascript"> var d = {}; d.winw = $(window).width();   $( window ).resize(function() {     d.winw = $(window).width();     $('.show-window-width').html(d.winw);     ismobilenav(); });  function ismobilenav() {     if(d.winw<=400) {         $('ul.nav').addclass('mobile-nav');     } else {         $('ul.nav').removeclass('mobile-nav');     } }   $(document).ready(function(){      //$('ul.nav').addclass("mobile-nav");      $('ul.nav.mobile-nav > li > a').on('click',function(e) {             e.preventdefault();             alert("here");     }); }); </script>   <style> ul.nav {     background-color:yellow; } ul.nav.mobile-nav {     background-color:orange; } </style> 

you must use event-delegation since elements class dynamic.

$(document).on('click','ul.nav.mobile-nav > li > a',function(e) {         e.preventdefault();         alert("here"); }); 

demo


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -