Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
HTML onafterprint Event Attribute
Next article icon

HTML Event Attributes Reference

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

HTML event attributes are used to define actions that should occur when specific events are triggered on a webpage.

  • They enable interaction between the user and the system, like clicking a button or resizing a window.
  • Event attributes are added to HTML elements to specify the event type and action.
  • They enhance dynamic behavior and improve user experience on a webpage.
HTML
<html> <head> 	<script> 		function displayMessage() { 			alert("Button was clicked!"); 		} 	</script> </head> <body> 	<button onclick="displayMessage()">Click Me!</button> </body> </html> 
  • <button onclick=”displayMessage()”>Click Me!</button>: The <button> element includes the onclick attribute, which is set to call the displayMessage() function when the button is clicked.
  • function displayMessage() { alert(“Button was clicked!”); }: This JavaScript function displays an alert box with the message “Button was clicked!” when invoked.

The complete list of Event Attributes are given below:

Window Event Attributes

Description

onafterprintUsed with onbeforeprint to perform actions after the page is printed.
onbeforeprintThe alert message display before the print dialogue box appears.
onbeforeunloadThe onbeforeunload event run when the document is about to be unloaded.
onerrorThis attribute works when an error occurs while loading an external file.
onhashchangeThis attribute works when there has been changes to the anchor part.
onloadThis attribute works when an object has been loaded.
onofflineThe onoffline event attribute works when the browser work in offline mode.
ononlineThe ononline event attribute works when the browser starts working in online mode.
onpageshowThis event occurs when a user navigates to a website.
onresizeThe onresize event attribute is triggered each time when resize the browser window size.
onunloadFired when the document is unloaded (e.g., when navigating away).


Form Event Attributes

Form Event Attributes

Description

onblurThis attribute is mostly used in Form validation code.
onchangeThe onchange event attribute works when the value of the element changes and select the new value from the List.
oncontextmenuThis attribute works when the user right-clicks on an element to open the context menu.
onfocusThis event attribute is mostly used with <input>, <select>, <a> elements.
oninputThis attribute works when it gets user input value.
oninvalidThe oninvalid event attribute works when an input field values are invalid or empty.
onresetThe onreset event attribute in HTML is triggered when reset the form.
onsearchWhen a user presses the ENTER button or click on the x button.
onselectThe onselect event attribute works when some text has been selected in an element.
onsubmitThe onsubmit event attribute in HTML is triggered when a form is submitted.

 

 

Keyboard Event Attributes

Keyboard Event Attributes

Description

onkeydownThe onkeydown event is triggered when the user presses any key on the keyboard.
onkeypressThis attribute fires when a user presses a key on the Keyboard.
onkeyupThis onkeyup event attribute works when the user releases the key from the keyboard.

Mouse Event Attributes

Mouse Event Attributes

Description

onclickThe onclick event attribute in HTML works when the user clicks on the button.
ondblclickThis Attribute Event occurs when a user fires mouse Double click on the Element.
onmousedownwhen a mouse button is pressed down on the element
onmousemoveThe onmousemove attribute works when the pointer moves over an element.
onmouseoutwhen the mouse pointer moves out of the specified element.
onmouseoverThe onmouseover event attribute works when the mouse pointer moves over the specified element.
onmouseupwhen a mouse button is released over the element.
onwheelwhen the wheel of pointer device is rolled up or down over an element.

Drag Event Attributes

Drag Event Attributes

Description

ondragwhen the element or text selection is being dragged in HTML
ondragendwhen the user finished the dragging of element. The drag and drop feature is common in HTML5
ondragenterThe ondragenter event attribute in HTML works when the content is draggable
ondragleaveThe ondragleave attribute works when a draggable element or text selection leaves a valid drop target
ondragstartHTML ondragstart Event Attribute is used when the user wants to drag the text or element.
ondropThe ondrop event attribute is used to drag an element or text and drop it into a valid droppable location or target.
onscrollThis onscroll attribute works when an element scrollbar is being scrolled.

Clipboard Events Attributes

Clipboard Events Attributes

Description

oncopyThis attribute fires when the user copied the content present in an element.
oncutThis attribute fires when the user cut or delete the content that has been present in the element.
onpasteThe onpaste attribute works when some content are paste in an element.

Misc Events Attributes

Misc Events Attributes

Description

ontoggleThe ontoggle event is triggered when the user open or close the <details> element.

Best Practices for Using HTML Event Attributes

  • Prefer External JavaScript: Instead of embedding JavaScript directly within HTML event attributes, link to external JavaScript files to enhance code organization and maintainability.
  • Use Event Listeners: Implement event listeners in JavaScript rather than inline event attributes to separate structure from behavior, improving code clarity.
  • Avoid Overuse: Limit the number of event attributes to prevent performance issues and maintain a responsive user interface.


Next Article
HTML onafterprint Event Attribute
author
kartik
Improve
Article Tags :
  • HTML
  • Web Technologies
  • HTML-Attributes
  • HTML-Event Attributes

Similar Reads

  • HTML Event Attributes Reference
    HTML event attributes are used to define actions that should occur when specific events are triggered on a webpage. They enable interaction between the user and the system, like clicking a button or resizing a window.Event attributes are added to HTML elements to specify the event type and action.Th
    5 min read
  • Window Event

    • HTML onafterprint Event Attribute
      The HTML onafterprint Event Attribute works when a page has started printing, or if the print dialogue box has been closed. This attribute is used together with the onbeforeprint attribute. This attribute is typically associated with the window or document element and is part of the WindowEventHandl
      2 min read

    • HTML onbeforeprint Event Attribute
      The onbeforeprint event fires when a page is about to be printed before the print dialog box appears. This event can be used to perform actions before printing, such as hiding unnecessary elements or preparing the page for printing. Syntax: <element onbeforeprint = "script">Supported Tags:<
      2 min read

    • HTML onbeforeunload Event Attribute
      The onbeforeunload event runs when the document is about to be unloaded, thus, allowing to display message in a dialog box to inform the user whether a user wants to stay or leave the current page, i.e., this event triggers just before a user leaves a webpage, and helps to prevent an accidental clos
      1 min read

    • HTML onerror Event Attribute
      The onerror event works when an error occurs while loading an external file. The external file may contain a document file or an image file. It simply activates when an error occurs during the loading of an external resource and It is commonly used to handle issues with image loading, script executi
      2 min read

    • HTML onhashchange Event Attribute
      HTML onhashchange Event Attribute works when there have been changes to the anchor part. The anchor part starts with the '#' symbol of the current URL. It also enables the execution of a script or function on hash change and is used for dynamic web applications updating without full page reload. In
      2 min read

    • HTML onload Event Attribute
      The onload event attribute works when an object has been loaded. This attribute is mostly used within the <body> element to execute a script. It can be used with other elements as well. This attribute is used to check the visitor's browser type and browser version and load the proper version o
      2 min read

    • HTML onoffline Event Attribute
      The onoffline event attribute works when the browser works in offline mode. It is the opposite of ononline event attribute. It is activated when the device loses its internet connection. It is also useful for adapting the web application's behavior when network connectivity is lost. Note: The `onoff
      1 min read

    • HTML | ononline Event Attribute
      The ononline event attribute works when the browser starts working in online mode. it is opposite to onoffline event attribute.Supported Tags <body> syntax <element ononline = "script"> Attribute Value: This event attribute contains single value script which works when browser working in
      1 min read

    • HTML onpageshow Event Attribute
      The onpageshow event occurs when a user navigates to a website. This event is quite similar to onload event but it occurs after the onload event. It occurs every time, when the page is loaded whereas the onload event does not occur when the page is loaded from the cache. When a page is loaded or res
      2 min read

    • HTML onresize Event Attribute
      The onresize event attribute is triggered each time when resizing the browser window size. It is generally used for responsive design, dynamically adjusting content layout during window resizing. To obtain the dimensions of an element, use the clientWidth, clientHeight, innerWidth, outerWidth, outer
      2 min read

    • HTML onunload Event Attribute
      The onunload event attribute works when the document is being unloaded i.e. it occurs when the browser has been closed. It is mostly used when the user opens a link and submits the form and closes the browser window. Basically, it is: Fired when a user navigates away from a page or closes the browse
      1 min read

    Form Event

    • HTML onblur Attribute
      This attribute fires at that moment when the element loses focus. This attribute is mostly used in the Form validation code. This attribute is the opposite of onfocus attribute.Supported Tags: It supports all HTML elements except these elements which are given below : <base> <bdo> <br
      2 min read

    • HTML onchange Event Attribute
      The onchange event attribute works when the value of the element changes and select the new value from the List. It is a part of the event attribute. It is similar to oninput event attribute. But the difference is that oninput attribute event occurs immediately after the value of the element changes
      2 min read

    • HTML | oncontextmenu Event Attribute
      This attribute works when the user right-clicks on an element to open the context menu.Supported Tags: It supports all HTML elements. Syntax: <element oncontextmenu="script"> Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called.
      1 min read

    • HTML onfocus Event Attribute
      This onfocus attribute works when the element gets focused. This event attribute is mostly used with <input>, <select>, <a> elements. Supported Tags: It supports all HTML elements which are given below: <base> <bdo> <br> <head> <html> <iframe>
      1 min read

    • HTML oninput Event Attribute
      This attribute works when it gets user input value. This attribute mainly fires when the user change the value of <input> and <textarea> element. This attribute is quite similar to onchange attribute but the basic difference is that the oninput event attribute occurs immediately when the
      1 min read

    • HTML oninvalid Event Attribute
      The oninvalid event attribute works when input field values are invalid or empty. The script runs when a user clicks on the submit button. The required input field must be filled out before submitting it. It is basically triggered when a form element encounters invalid input and provides real-time f
      2 min read

    • HTML onreset Event Attribute
      The onreset event attribute in HTML is triggered when reset the form. This Attribute works with form tag and is triggered when a form is reset, either by user action or programmatically. It also allows the execution of scripts or functions to handle actions after form reset. Syntax:<form onreset
      2 min read

    • HTML onsearch Event Attribute
      This onsearch attribute works when a user presses the ENTER button or clicks on the x button. This event works with <input> element. Supported Tags: <input type="search">Syntax: <element onsearch="script">Attribute: This event contains single attribute script which contains the scr
      1 min read

    • HTML onselect Event Attribute
      The onselect event attribute works when some text has been selected in an element. It is the part of event attribute. It is basically activated when text within an input or textarea is selected and enhances user interaction by responding to text highlighting. Syntax: <element onselect = "script"
      2 min read

    • HTML onsubmit Event Attribute
      The onsubmit event attribute in HTML is triggered when a form is submitted. It is basically triggered when a form is submitted, either by clicking a submit button or using JavaScript. It is also useful for validating form data or performing actions before any submission and ensures better control an
      2 min read

    Keyboard Event

    • HTML onkeydown Event Attribute
      The onkeydown event attribute works when the user presses any key from the keyboard. The onkeydown event is triggered when the down key is pressed down and used for capturing and responding to key events. Syntax: <element onkeydown = "script">Supported Tags: It supports all HTML elements EXCEP
      2 min read

    • HTML onkeypress Attribute
      This attribute fires when a user presses a key on the Keyboard. This event attribute can not be used for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers.Supported Tags: All HTML elements, EXCEPT: <base><bdo><br><head><html><iframe><meta><param>
      2 min read

    • HTML onkeyup Event Attribute
      The HTML onkeyup event attribute executes JavaScript code when a user releases a key after pressing it within an input field or textarea, enabling dynamic responses to keyboard input. It also enables real-time interaction with user input in web forms or applications and is commonly used for live sea
      2 min read

    Mouse Event

    • HTML onclick Event Attribute
      The onclick event attribute in HTML triggers when the user clicks on an element. It executes a script or function upon a click event and is commonly used for interactive elements like buttons and links. Syntax<element onclick = "script">Attribute Value This attribute contains a single value sc
      2 min read

    • HTML ondblclick Event Attribute
      The ondblclick Attribute Event occurs when a user fires the mouse Double click on Element. It is triggered when a user double-clicks on an HTML element and is used to initiate actions or functions in response to a double-click event. It also enables interactive features like opening pop-ups, display
      2 min read

    • HTML onmousedown Attribute
      This onmousedown attribute will fire when a mouse button is pressed down on the element and the order of events occur related to the onmousedown event(for the left/mouse button) onmousedownonmouseuponclickSupported Tags: It supports all HTML elements. Syntax: <element onmousedown="script">Attr
      2 min read

    • HTML onmousemove Event Attribute
      The HTML onmousemove event attribute triggers JavaScript code when the mouse pointer moves over an element, allowing interaction based on mouse movement within the webpage. Syntax:  <element onmousemove = "script">Attribute Value: This attribute contains single value script which works when on
      1 min read

    • HTML onmouseout Event Attribute
      The HTML  onmouseout event attribute works when the mouse pointer moves out of the specified element. It activates when the mouse pointer moves out of an HTML element. When a user moves the mouse away, this can be utilized to activate specific actions or functions and enables dynamic changes in resp
      2 min read

    • HTML onmouseover Event Attribute
      The onmouseover event attribute works when the mouse pointer moves over the specified element. It is basically: Activated when the mouse pointer enters the designated HTML element.Enables executing JavaScript code when the mouse hovers over the element.Ideal for enhancing user experience by providin
      2 min read

    • HTML | onmouseup Event Attribute
      This attribute fires when a mouse button is released over the element. The order of events occur related to the onmouseup event. onmousedownonmouseuponclick Supported Tags: All HTML elements, EXCEPT: <base> <bdo><br><head><html><iframe><meta><param>
      1 min read

    • HTML | onwheel Event Attribute
      This attribute works when the wheel of pointer device is rolled up or down over an element. The onwheel attribute also works when the user scrolls or zooms on an element by using mouse or touchpad. Supported Tags: It supports all HTML elements. Syntax: <element onwheel = "script"> Attribute Va
      1 min read

    Drag Event

    • HTML ondrag Event Attribute
      The ondrag event attribute triggers when the element or text selection is being dragged in HTML. This event is very similar to the drag-and-drop event. This will triggered during a drag operation when an HTML element is being dragged. Syntax<element ondrag = "script"> Supported TagsIt supports
      1 min read

    • HTML ondragend Event Attribute
      The ondragend event attribute works when the user finishes the dragging of an element. The drag-and-drop feature is common in HTML5. Any element can be made draggable by using the HTML5 draggable attribute. It is fired when the user releases the mouse button or touch input also used for integrating
      3 min read

    • HTML ondragenter Event Attribute
      The ondragenter Event attribute in HTML works when the content is draggable. An element is made by setting the draggable attribute value to true. The draggable attribute can take only true or false values and activates when a dragged element enters a valid drop target. Syntax<element ondragenter
      2 min read

    • HTML ondragleave Event Attribute
      The ondragleave attribute works when a draggable element or text selection leaves a valid drop target. It helps in dragging the elements and get to know the entering or leaving a drop target. Drag and drop feature is very popular in HTML5. Use CSS property when the element is draggable and enter int
      3 min read

    • HTML ondragstart Event Attribute
      HTML ondragstart Event Attribute is used when the user wants to drag the text or element. It is simply the process in which we press on the desired text to drag and drop them to a different location. Basically, it Initiates when the user starts dragging an element and is used to set data to be trans
      3 min read

    • HTML ondrop Event Attribute
      The ondrop event attribute is used to drag an element or text and drop it into a valid droppable location or target. The drag and drop is a common feature of HTML 5. There are different events that are used and occur before ondrop event. Events occur on the draggable targetEvents occur on the drop t
      3 min read

    • HTML onscroll Attribute
      The onscroll attribute works when an element scrollbar is being scrolled. To create a scrollbar in an element, use the CSS overflow property. It is triggered when the content within an HTML element is scrolled and used to implement dynamic behaviors based on the scroll position and employed for crea
      4 min read

    Clipboard Events

    • HTML oncopy Attribute
      The oncopy Attribute attribute fires when the user copies the content present in an element. The oncopy attribute is used with <img>, <input>, <p> etc elements. It basically triggers on user-initiated copy operations and customizes behavior when content is copied. Syntax<element
      3 min read

    • HTML oncut Attribute
      This attribute fires when the user cut or deletes the content that has been present in the element. It is a Boolean-type attribute. This attribute is supported by all HTML elements but it is possible for that element which has a ContentEditable attribute set to "true". Note:There are 3 ways to cut t
      3 min read

    • HTML onpaste Event Attribute
      The onpaste attribute works when some content is pasted in an element. This event attribute is supported by all HTML elements. It is mostly used with <input> element.There are three ways to paste the content in HTML elements which are listed below:  Use CTRL + V keySelect "Paste" from the edit
      2 min read

    Misc Events

    • HTML ontoggle Event Attribute
      The ontoggle event is triggered when the user opens or closes the <details> element. The <details> element is used to provide additional information/details that the user can view or hide details according to requirements. Syntax<details ontoggle = "script">Supported Tag<details
      2 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences