Ad

How To Start/stop Marquee On Enter Key Press?

- 1 answer

following is the code snippet for marquee to stop on mouse click down and start on mouse click up. I need to change it to detect the event of pressing enter key from keyboard. How is that possible? thanks

<marquee behavior="scroll" direction="left" onmousedown="this.stop();" onmouseup="this.start();">
Go on... click me (and hold the mouse down)!
</marquee>
Ad

Answer

 <!DOCTYPE html>
 <html>
      <body>
         <marquee id="marquee" behavior="scroll" direction="left" 
          onmousedown="this.stop();" onmouseup="this.start();">
          Go on... click me (and hold the mouse down)!
          </marquee>
          <script>
                 if(document.readyState){
                     const marquee = document.getElementById('marquee');
                     document.body.addEventListener('keydown',function(event){
                     if(event.key == 'Enter'){
                          marquee.stop()
                     }
                 })
                     document.body.addEventListener('keyup',function(event){
                         if(event.key == 'Enter'){
                     marquee.start()
        }
    })
}

</script>
</body>
</html> 
Ad
source: stackoverflow.com
Ad