0

I am working with material design lite in my project and I need to attach a function to the click event of the drawer button. I thought it was trivial, so I added this code:

$(document).ready(function(){ 
    $('.mdl-layout__drawer-button).on('click', function(){
       console.log('click');
});

but then I discovered that the drawer button is added by mdl.js as the very last element after document.ready, so when I run my code, the button is not yet ready.

Any idea on how to get the selector when it is ready?

Fabio Marzocca
  • 1,573
  • 16
  • 36

1 Answers1

2

You need delegation : https://learn.jquery.com/events/event-delegation/

$(document.body).on('click', '.mdl-layout__drawer-button', function(){
  console.log('click');
});
Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44