MCQ Practice Single Best Answer Topic: Basic JavaScript MCQ

Q What circumstance is connected to the keyboard?

Question ID
#4851
Subchapter
Basic JavaScript MCQ
Action
Choose one option below

Choose Your Answer

Click an option to check whether your answer is correct.

  • A onfocus
  • B onclick
  • C onkeydown
  • D onkeypress
Correct Answer: D

Explanation

The onkeydown, onkeypress, and onkeyup events are all related to the keyboard in JavaScript.
The onkeydown event is triggered when a user presses a key on the keyboard.
The onkeypress event is triggered when a user presses and releases a key on the keyboard.
The onkeyup event is triggered when a user releases a key on the keyboard.
Here's an example of how you might use these events in your code:

document.addEventListener('keydown', function(event) {
console.log('Keydown event:', event);
});

document.addEventListener('keypress', function(event) {
console.log('Keypress event:', event);
});

document.addEventListener('keyup', function(event) {
console.log('Keyup event:', event);
});


The onfocus event, on the other hand, is related to an element on the page receiving focus. It is triggered when an element receives focus, either by a user clicking on it or tabbing to it using the keyboard.

Here's an example of how you might use the onfocus event:

let input = document.querySelector('input');

input.addEventListener('focus', function() {
console.log('Input received focus');
});

Share This Question

Share this MCQ with your friends or study group.