%%javascript function toggler(){ if(window.already_toggling){ // Don't add multiple buttons. return 0 } let btn = $('.input').append('<button>Toggle Code</button>') .children('button'); btn.on('click', function(e){ let tgt = e.currentTarget; $(tgt).parent().children('.inner_cell').toggle() }) window.already_toggling = true; } // Since javascript cells are executed as soon as we load // the notebook (if it's trusted), and this cell might be at the // top of the notebook (so is executed first), we need to // allow time for all of the other code cells to load before // running. Let's give it 5 seconds. setTimeout(toggler, 5000);