Question #52   Submitted by Answiki on 06/06/2020 at 03:44:58 PM UTC

In JavaScript, how to handle Tabulation key press?

Answer   Submitted by Answiki on 06/06/2020 at 03:44:34 PM UTC

To handle the Tab key press event in JavaScript, use the following code:

document.querySelector('body').addEventListener('keydown', function(event)
{
	// Get key code
	var code = event.keyCode || event.which;
	
	// Check if the key pressed is Tab (code = 9)
	if (code === 9) {  

		// Keep the following line to prevent the default behavior of the Tab key
		event.preventDefault();

		// The tab key has been pressed
		// Do some awesome stuff here
		//...
	}
});

3 events in history
Question by Answiki 06/06/2020 at 03:44:58 PM
In JavaScript, how to handle Tabulation key press?
Answer by Answiki on 06/06/2020 at 03:44:34 PM

To handle the Tab key press event in JavaScript, use the following code:

document.querySelector('body').addEventListener('keydown', function(event)
{
	// Get key code
	var code = event.keyCode || event.which;
	
	// Check if the key pressed is Tab (code = 9)
	if (code === 9) {  

		// Keep the following line to prevent the default behavior of the Tab key
		event.preventDefault();

		// The tab key has been pressed
		// Do some awesome stuff here
		//...
	}
});

Question by Answiki 06/06/2020 at 03:40:34 PM
In JavaScript, how to handle Tab key press?
# ID Query URL Count

Icons proudly provided by Friconix.