Skip to content

divyanshrajpoot9/Generating-the-keyboard-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Generating-the-keyboard-code

Java Script DOM Manipulation:

The Document Object Model (DOM) is a programming interface for web documents. It provides a structured representation of a web page, allowing you to access and manipulate its elements and content using JavaScript. Here are some basic DOM properties and methods. document: The document object represents the entire HTML document and serves as the entry point to the DOM. It provides properties and methods to access and modify the document's structure, content, and style.

Element Selection and Traversal:

getElementById(id): Retrieves an element by its unique id attribute.

getElementsByClassName(className): Returns a collection of elements with a specific class name.

getElementsByTagName(tagName): Returns a collection of elements with a specific tag name.

querySelector(selector): Returns the first element that matches the CSS selector.

querySelectorAll(selector): Returns a list of all elements that match the CSS selector.

This JavaScript code is used to display information related to keyboard events on an HTML document. We break down the code step by step:

  1. const outputVar = document.getElementById('output');

    • This line of code selects an HTML element with the ID 'output' and assigns it to the variable outputVar. This element is presumably where the output of the keyboard events will be displayed.
  2. console.log(outputVar);

    • This line logs the outputVar to the console, which can be helpful for debugging to check if the element has been successfully selected.
  3. function keydisplay(event) { ... }

    • This is a function definition named keydisplay. It takes one parameter, event, which represents a keyboard event.
  4. outputVar.innerHTML = ${event.key};

    • Inside the keydisplay function, this line sets the inner HTML of the element represented by outputVar to the value of event.key. event.key contains the character associated with the key that triggered the keyboard event. It is used to display the key that was pressed.
  5. document.addEventListener('keydown', keydisplay);

    • This line adds an event listener to the document object for the 'keydown' event. When a key is pressed, the keydisplay function will be called, updating the content of the 'output' element with the key that was pressed.
  6. function myFunction(event) { ... }

    • This is another function definition named myFunction. It takes one parameter, event, which represents a keyboard event.
  7. let value = event.keyCode;

    • Inside the myFunction function, this line retrieves the numeric key code of the key that triggered the event and assigns it to the variable value.
  8. document.getElementById("key-code-display").innerHTML = value;

    • This line sets the inner HTML of an element with the ID 'key-code-display' to the value stored in the value variable. This element is used to display the numeric key code of the key that was pressed.
  9. document.addEventListener('keypress', myFunction);

    • This line adds an event listener to the document object for the 'keypress' event. When a key is pressed and produces a character, the myFunction function will be called, updating the content of the 'key-code-display' element with the numeric key code.

In summary, this code listens for 'keydown' and 'keypress' events on the document, and it displays both the character associated with the key pressed and the numeric key code of the key in the respective HTML elements. This can be used to show the user which key they pressed and its corresponding key code.

image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published