Listen for all events in JavaScript

A more modern rewrite of @roman-bekkiev’s answer: Object.keys(window).forEach(key => { if (/^on/.test(key)) { window.addEventListener(key.slice(2), event => { console.log(event); }); } }); Note that you can further customize what you want to catch, for example: /^on(key|mouse)/.test(key)

How do I capture a key press (or keydown) event on a div element?

(1) Set the tabindex attribute: <div id=”mydiv” tabindex=”0″ /> (2) Bind to keydown: $(‘#mydiv’).on(‘keydown’, function(event) { //console.log(event.keyCode); switch(event.keyCode){ //….your actions for the keys ….. } }); To set the focus on start: $(function() { $(‘#mydiv’).focus(); }); To remove – if you don’t like it – the div focus border, set outline: none in the CSS. … Read more

Template literals like ‘some ${string}’ or “some ${string}” are not working

JavaScript template literals require backticks, not straight quotation marks. You need to use backticks (otherwise known as “grave accents” – which you’ll find next to the 1 key if you’re using a QWERTY keyboard) – rather than single quotes – to create a template literal. Backticks are common in many programming languages but may be … Read more