I have a pretty simple setup:
- button: toggles “toggled” class on the nav
- li is simplified holder for submenu: it should change aria attribute on click, but only when the nav has .toggled class on it
But for some reason this line doesn't work:
$('.toggled .has-children').on('click', function(e){
Is it because the class is added dynamically to the parent? How would I overcome this?
$(function() {
  
  var container = $('.nav');
  var button = $('.menu');
  
  //add remove class to nav
  button.on('click', function () {
      if(container.hasClass('toggled')) {
          container.removeClass('toggled');
      } else {
          container.addClass('toggled');
      }
  });
  // doesn't work?
  $('.toggled .has-children').on('click', function(e){
    e.preventDefault();
    $(this).attr('aria-expanded', 'true');
  })
  
});.toggled {
  color: blue;
}
.has-children[aria-expanded=true] {
  color: red;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav"> 
<button class="menu">Menu</button>
  <ul>
    <li class="has-children" aria-expanded="false">
      Has children
    </li>  
    
  </ul>
  
</nav> 
    