Skip to content

Aliyanshaikh66/JavaScript-Events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

JavaScript-Events

In JavaScript, an event represents an action or occurrence, such as a user clicking a button, submitting a form, pressing a key, or loading a page. Events allow you to make your web pages interactive by responding to user interactions. Here are a few commonly used JavaScript events and how you can use them:

1. Click Event

Triggered when an element is clicked.

document.getElementById("myButton").addEventListener("click", function() {
    alert("Button was clicked!");
});

2. Mouse Events

Events like mouseover, mouseout, etc., respond to mouse actions.

document.getElementById("myDiv").addEventListener("mouseover", function() {
    this.style.backgroundColor = "lightblue";
});

3. Keyboard Events

Triggered when a user presses a key (keydown, keyup, keypress).

document.addEventListener("keydown", function(event) {
    console.log("Key pressed: " + event.key);
});

4. Load Event

Fires when the page has completely loaded.

window.addEventListener("load", function() {
    console.log("Page fully loaded");
});

5. Form Events

Events like submit and change work with form inputs.

document.getElementById("myForm").addEventListener("submit", function(event) {
    event.preventDefault(); // Prevents form submission
    alert("Form submitted!");
});

6. Focus and Blur Events

Triggered when an input element gains (focus) or loses (blur) focus.

document.getElementById("myInput").addEventListener("focus", function() {
    this.style.backgroundColor = "yellow";
});

7. Double Click Event (dblclick)

Triggered when an element is double-clicked.

document.getElementById("myButton").addEventListener("dblclick", function() {
    alert("Button was double-clicked!");
});

8. Context Menu Event (contextmenu)

Triggered when the user right-clicks on an element, opening the context menu.

document.getElementById("myDiv").addEventListener("contextmenu", function(event) {
    event.preventDefault(); // Prevents the default context menu
    alert("Custom right-click menu triggered!");
});

9. Scroll Event

Triggered when the user scrolls in the browser window or a scrollable element.

window.addEventListener("scroll", function() {
    console.log("Page scrolled");
});

10. Resize Event

Fires when the browser window is resized.

window.addEventListener("resize", function() {
    console.log("Window resized to " + window.innerWidth + " x " + window.innerHeight);
});

11. Input Event

Triggered whenever the value of an <input> or <textarea> element changes.

document.getElementById("myInput").addEventListener("input", function() {
    console.log("Input changed to: " + this.value);
});

12. Change Event

Similar to the input event but only fires when the user leaves the input field.

document.getElementById("mySelect").addEventListener("change", function() {
    console.log("Selection changed to: " + this.value);
});

13. Drag and Drop Events

Used to implement drag-and-drop functionality.

  • dragstart: Fires when the user starts dragging an element.
  • dragover: Fires when a dragged element is over a drop target.
  • drop: Fires when the dragged element is dropped.
// HTML: <div id="draggable" draggable="true">Drag me</div>
document.getElementById("draggable").addEventListener("dragstart", function(event) {
    event.dataTransfer.setData("text/plain", event.target.id);
});
document.getElementById("dropZone").addEventListener("drop", function(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("text");
    event.target.appendChild(document.getElementById(data));
});
document.getElementById("dropZone").addEventListener("dragover", function(event) {
    event.preventDefault();
});

14. Animation Events

Used to detect CSS animations.

  • animationstart: Fired when the animation starts.
  • animationend: Fired when the animation ends.
  • animationiteration: Fired each time the animation repeats.
document.getElementById("animatedDiv").addEventListener("animationend", function() {
    console.log("Animation ended!");
});

15. Transition Events

Triggered during CSS transitions.

  • transitionstart: Fired when the transition starts.
  • transitionend: Fired when the transition completes.
document.getElementById("transitionDiv").addEventListener("transitionend", function() {
    console.log("Transition ended!");
});

16. Focus Events

Detect when an element gains or loses focus.

  • focusin: Similar to focus but also bubbles.
  • focusout: Similar to blur but also bubbles.
document.getElementById("myInput").addEventListener("focusin", function() {
    console.log("Input focused in!");
});

17. Touch Events (for mobile devices)

Handle touch-based interactions.

  • touchstart: When a finger touches the screen.
  • touchmove: When a finger slides on the screen.
  • touchend: When a finger is removed from the screen.
document.getElementById("touchArea").addEventListener("touchstart", function(event) {
    console.log("Touch started!");
});

18. Beforeunload Event

Fired when the user attempts to leave the page, often used to show a confirmation dialog.

window.addEventListener("beforeunload", function(event) {
    event.preventDefault();
    event.returnValue = '';
});

19. Error Event

Fired when an error occurs while loading an external file (like an image or script).

document.getElementById("myImage").addEventListener("error", function() {
    console.log("Error loading image!");
});

20. Custom Events

You can create your own custom events using the CustomEvent constructor.

var customEvent = new CustomEvent("myCustomEvent", { detail: { message: "Hello World" } });
document.addEventListener("myCustomEvent", function(event) {
    console.log(event.detail.message);
});
document.dispatchEvent(customEvent);

Summary

JavaScript events allow you to interact with and control the user's experience on a web page. Using the right events for different situations will help you create dynamic and engaging web applications. Let me know if you need examples of specific event types or more details on any of these!

How to Add Events

You can attach events using:

  • Inline HTML (not recommended): <button onclick="myFunction()">Click me</button>
  • JavaScript Event Listeners (preferred): element.addEventListener("event", function)

Of course! Here’s a simplified overview of JavaScript events, merged and organized to help you easily explain them to your students:


JavaScript Events: A Quick Guide

In JavaScript, events are actions or occurrences that happen in the web page, like clicks, typing, or scrolling. We use events to make web pages interactive and respond to user actions.

Commonly Used JavaScript Events

  1. Click Event

    • Triggered when the user clicks on an element.
    • Example:
      document.getElementById("myButton").addEventListener("click", function() {
          alert("Button clicked!");
      });
  2. Double Click Event (dblclick)

    • Triggered when the user double-clicks on an element.
    • Example:
      document.getElementById("myButton").addEventListener("dblclick", function() {
          alert("Button double-clicked!");
      });
  3. Mouse Events

    • Events triggered by mouse actions, like hovering.
    • Examples:
      • mouseover: When the mouse moves over an element.
      • mouseout: When the mouse leaves an element.
      document.getElementById("myDiv").addEventListener("mouseover", function() {
          this.style.backgroundColor = "lightblue";
      });
  4. Keyboard Events

    • Triggered when a key is pressed.
    • Examples:
      • keydown: When a key is pressed down.
      • keyup: When a key is released.
      document.addEventListener("keydown", function(event) {
          console.log("Key pressed: " + event.key);
      });
  5. Form Events

    • Events triggered by form actions, like submitting or changing an input.
    • Examples:
      • submit: When a form is submitted.
      • change: When a form input changes.
      document.getElementById("myForm").addEventListener("submit", function(event) {
          event.preventDefault(); // Stops form submission
          alert("Form submitted!");
      });
  6. Focus Events

    • Triggered when an element, like an input, gains or loses focus.
    • Examples:
      • focus: When an element gains focus.
      • blur: When it loses focus.
      document.getElementById("myInput").addEventListener("focus", function() {
          this.style.backgroundColor = "yellow";
      });
  7. Scroll Event

    • Triggered when the page or a scrollable element is scrolled.
    • Example:
      window.addEventListener("scroll", function() {
          console.log("Page scrolled!");
      });
  8. Resize Event

    • Triggered when the browser window is resized.
    • Example:
      window.addEventListener("resize", function() {
          console.log("Window resized!");
      });
  9. Touch Events (Mobile)

    • Triggered by touch actions on mobile, like tapping or swiping.
    • Examples:
      • touchstart: When a finger touches the screen.
      • touchmove: When a finger slides.
      • touchend: When the finger is removed.
      document.getElementById("touchArea").addEventListener("touchstart", function() {
          console.log("Screen touched!");
      });
  10. Custom Events

    • You can create your own events for specific actions in your code.
    • Example:
      var customEvent = new CustomEvent("myCustomEvent", { detail: { message: "Hello" } });
      document.addEventListener("myCustomEvent", function(event) {
          console.log(event.detail.message);
      });
      document.dispatchEvent(customEvent);

Tips for Using Events

  • Event Listener: Use addEventListener to attach events, which keeps your code organized.
  • Prevent Default: Use event.preventDefault() to stop the default action, like preventing form submission.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published