The Wayback Machine - https://web.archive.org/web/20240910141850/https://www.geeksforgeeks.org/html-event-attributes/
Open In App

HTML Event Attributes Reference

Last Updated : 18 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Events are actions that happen in the browser when the user does something For example when users click on the mouse or type something on the keyboard.

HTML Event Attributes: In HTML, we can use event actions on the browser, and with the help of the event, the user responds to the system. Event Attributes can be used with HTML elements to perform various actions.

Example :

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>onbeforeprint attribute</title>
        <style>
            body {
                text-align:center;
            }
            h1 {
                color:green;
            }
        </style>
    </head>
    <body onbeforeprint="myFunction()">
        <h1>GeeksforGeeks</h1>
        <h2>onbeforeprint attribute</h2>
        <!-- The script run when page will ready to print -->
        <script>
            function myFunction() {
                alert("This document is ready to be printed");
            }
        </script>
    </body>
</html>


Output:

The complete list of Event Attributes are given below:

Window Event Attributes

Window Event Attributes

Description

Example

onafterprint This attribute is used together with the onbeforeprint attribute.

onbeforeprint The alert message display before the print dialogue box appears.

onbeforeunload The onbeforeunload event run when the document is about to be unloaded.

onerror This attribute works when an error occurs while loading an external file.

onhashchange This attribute works when there has been changes to the anchor part.

onload This attribute works when an object has been loaded.

onoffline The onoffline event attribute works when the browser work in offline mode.
ononline The ononline event attribute works when the browser starts working in online mode.
onpageshow This event occurs when a user navigates to a website.
onresize The onresize event attribute is triggered each time when resize the browser window size.

onunload The onunload event attribute works when the document is being unloaded i.e.

 

 

Form Event Attributes

Form Event Attributes

Description

Example

onblur This attribute is mostly used in Form validation code.

onchange The onchange event attribute works when the value of the element changes and select the new value from the List.

oncontextmenu This attribute works when the user right-clicks on an element to open the context menu.

onfocus This event attribute is mostly used with <input>, <select>, <a> elements.

oninput This attribute works when it gets user input value.

oninvalid The oninvalid event attribute works when an input field values are invalid or empty.

onreset The onreset event attribute in HTML is triggered when reset the form.

onsearch When a user presses the ENTER button or click on the x button.

onselect The onselect event attribute works when some text has been selected in an element.

onsubmit The onsubmit event attribute in HTML is triggered when a form is submitted.

 

 

Keyboard Event Attributes

Keyboard Event Attributes

Description

Example

onkeydown This onkeydown event attribute works when user presses any key from the keyboard

onkeypress This attribute fires when a user presses a key on the Keyboard.

onkeyup This onkeyup event attribute works when the user releases the key from the keyboard.

Mouse Event Attributes

Mouse Event Attributes

Description

Example

onclick The onclick event attribute in HTML works when the user clicks on the button.

ondblclick This Attribute Event occurs when a user fires mouse Double click on the Element.

onmousedown when a mouse button is pressed down on the element

onmousemove The onmousemove attribute works when the pointer moves over an element.

onmouseout when the mouse pointer moves out of the specified element.

onmouseover The onmouseover event attribute works when the mouse pointer moves over the specified element.

onmouseup when a mouse button is released over the element.

onwheel when the wheel of pointer device is rolled up or down over an element.

Drag Event Attributes

Drag Event Attributes

Description

Example

ondrag when the element or text selection is being dragged in HTML

ondragend when the user finished the dragging of element. The drag and drop feature is common in HTML5

ondragenter The ondragenter event attribute in HTML works when the content is draggable

ondragleave The ondragleave attribute works when a draggable element or text selection leaves a valid drop target

ondragstart HTML ondragstart Event Attribute is used when the user wants to drag the text or element.

ondrop The ondrop event attribute is used to drag an element or text and drop it into a valid droppable location or target.

onscroll This onscroll attribute works when an element scrollbar is being scrolled.

Clipboard Events Attributes

Clipboard Events Attributes

Description

EDescription

oncopy This attribute fires when the user copied the content present in an element.

oncut This attribute fires when the user cut or delete the content that has been present in the element.

onpaste The onpaste attribute works when some content are paste in an element.

Misc Events Attributes

Misc Events Attributes

Description

Example

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



Similar Reads

HTML Attributes Complete Reference
HTML attributes are special words placed inside the opening tags of HTML elements to define their characteristics. Each attribute consists of two parts: the attribute name and its value, separated by an equal sign (=) and enclosed in double quotes (" "). Syntax of HTML Attribute&lt;tag_name attribute_name="value"&gt; Contents... &lt;/tag_name&gt;Ov
7 min read
How to remove all attributes of an HTML element using jQuery ?
In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, function() { this.ownerElement.removeAttributeNode(
1 min read
HTML DOM attributes Property
The attributes property in HTML DOM returns the group of node attributes specified by NamedNodeMap objects. The NamedNodeMap object represents the collection of attribute objects and can be accessed by index number. The index number starts at 0. Syntax: node.attributes Return Value: It returns the NamedNodeMap object which is the collection of node
2 min read
Difference between 'hidden' and 'aria-hidden' attributes in HTML
The HyperText Markup Language (HTML) is a powerful web development tool combined with CSS and JavaScript. Apart from these, HTML also uses Accessible Rich Internet Application (ARIA) to make web content affable for a person with disabilities. Although ARIA is beneficial, there are keywords common to both HTML and ARIA, creating perplexity among ama
2 min read
Why are dashes preferred for CSS selectors / HTML attributes ?
CSS is a hyphen-delimited syntax language which means while writing HTML attributes we can use hyphen(-). Example: font-size, line-height, background-color, etc... There are various reasons to preferring hyphen(-): In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, ...), but preference is given based on its ea
2 min read
What is the difference between properties and attributes in HTML?
HTML is the standard language for creating documents that are intended to be displayed on web browsers. It is very usual to get confused with attributes and properties while performing DOM object manipulation in JavaScript. Before learning about the differences, let us first define the Document Object Model. DOM: It is the acronym for the Document
4 min read
How to add attributes to an HTML element in jQuery?
Given an HTML document containing some elements and the task is to add the attributes to the HTML elements. Approach: Given an HTML document containing &lt;label&gt;, &lt;input&gt; and &lt;button&gt; elements. The &lt;input&gt; element contains the type and id attribute. When user click on the button, the jQuery function is called. We use $(selecto
1 min read
Placeholder vs Value Attributes in HTML
Placeholder Attribute:The placeholder attribute specifies a short hint that describes the expected value of an input field/textarea. The short hint is revealed in the field before the user enters a value. It is just a temporary hint and has nothing to do with logical executions in the backend. Syntax: &lt;input placeholder = "text"&gt; &lt;textarea
3 min read
What are HTML tags & Attributes mandatory for a good Web page in SEO ?
Search engine optimization (SEO) is the process that involves enhancing the visibility of a website on search engines. This is achieved by increasing the website's ranking on search engine results pages, such as Google. The higher the website appears on these pages, the more likely it is to receive a larger volume of traffic. SEO involves understan
5 min read
Attributes that work differently between React and HTML
There is a minor difference between React(JSX) and HTML attributes. Like for example, the HTML attributes like class and for are replaced with className and htmlFor in React. There are a number of attributes that work differently between React and HTML. Grammar in JSX is mostly the same as in HTML, but there are subtle differences to watch out for.
3 min read
What is the purpose of the HTML "aria-*" attributes?
The HTML "aria-*" attributes, part of WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications), are used to enhance the accessibility of web content. These attributes provide additional information to assistive technologies, such as screen readers, in interpreting and presenting content to users with disabilities. Syntax&lt;b
1 min read
Difference Between Async & Defer Attributes in HTML
The async and defer attributes on a &lt;script&gt; tag in HTML are used to control the loading and execution behavior of external JavaScript files. While both of them have their own set of advantages and disadvantages, one should know which attribute is used at the right time. The difference between the async and defer attributes in HTML are as fol
5 min read
What are HTML Attributes ?
HTML attributes are the entities that provide the extra information about the tags. Attributes are specified using name and value pair. Some HTML tags are used without attributes while for some tags it's important to specify attributes along with them. In paired tags attributes are specified in the opening tag. The value of the attribute is specifi
3 min read
Difference between id and class Attributes in HTML
In HTML, the "id" attribute is employed to uniquely identify a specific element on a page. On the other hand, the "class" attribute is utilized to categorize and apply styles or scripts to multiple elements, enabling the application of common styles to various parts of the page where the class is assigned. HTML id AttributeThe id attribute is a uni
3 min read
How to Create Optional HTML attributes with Razor View Engine ?
It's common practice in web development to dynamically create HTML components with optional attributes in response to specific criteria. When working with ASP.NET Core apps and the Razor view engine, this necessity is especially prevalent. With Razor, users can easily generate dynamic web content by integrating C# code with HTML markup in an easy a
3 min read
How to use ARIA Attributes to Enhance Accessibility in HTML ?
Accessibility is a fundamental aspect of web development, ensuring that all users, regardless of ability, can access and interact with online content. HTML provides various features to improve accessibility, and one powerful tool is ARIA (Accessible Rich Internet Applications) attributes. ARIA attributes extend HTML semantics, providing additional
2 min read
How to Add and Remove HTML Attributes with jQuery?
Using jQuery, we can dynamically add and remove HTML attributes such as placeholder, style, and more to enhance the interactivity and functionality of web elements based on user actions. We will explore two different approaches to adding and removing HTML attributes with jQuery. Below are the possible approaches to add and remove HTML attributes wi
2 min read
HTML Global Attributes
HTML Global Attributes are attributes that can be used with any HTML element. They include id, class, style, title, lang, tabindex, and others, providing common functionality and enhancing element behavior and presentation across web pages. List of Global Attributes: Global Attributes Description Example accesskeyIt is the keyboard shortcuts to act
3 min read
Tags vs Elements vs Attributes in HTML
In HTML, tags represent the structural components of a document, such as &lt;h1&gt; for headings. Elements are formed by tags and encompass both the opening and closing tags along with the content. Attributes provide additional information or properties to elements, enhancing their functionality or appearance. Table of Content HTML Tags: HTML eleme
2 min read
Difference between "blank" and "_blank" target attributes in HTML
If you have ever noticed on the website that few links got opened either in a new tab or in a new window or sometimes, it might also happen that in some websites, click on the link, for the first time, it will open in a new tab &amp; that particular open tab will update &amp; reused every time whenever we click to the link. Both these scenarios can
3 min read
HTML Attributes
HTML attributes provide additional information about elements within an HTML document. Every HTML element can have attributes. Attributes are always defined in the start tag. They are specified using a name/value pair format, where the attribute name defines the property, and its value provides specific details, like name="value". These attributes
6 min read
HTML data-* Attributes
The HTML data-* attribute allows storing custom data for elements, enhancing interactivity in web applications through JavaScript without server-side involvement or AJAX calls. Syntax &lt;li data-book-author="Rabindra Nath Tagore"&gt; Gitanjali &lt;/li&gt;The data-* attributes can be used to define our own custom data attributes. It stores custom d
3 min read
jQuery Event Methods Complete Reference
An event refers to the actions performed by the site visitor during their interactivity with the website (or webpage). There can be various types of events such as The user clicks on the button.The user moves the mouse pointer over an image.The user pressed any key from the keyboard, etc. Syntax: $(selector).method(function) Example: C/C++ Code
6 min read
What is Event bubbling and Event Capturing in JavaScript ?
Event bubbling and event capturing are the two interesting concepts of JavaScript. Before diving deep into these fascinating concepts, let us first know about what an event listener is? An event listener is basically a function that waits for an event to occur. That event can be anything like a mouse click event, submitting a form, pressing keys of
6 min read
What is the use of the Event.preventDefault() method in Event Handling in JavaScript ?
The Event.preventDefault() method in JavaScript is used within event handlers to prevent the default behavior associated with an event from taking place. When an event occurs, such as a click on a link or the submission of a form, there is often a default action associated with that event. preventDefault() allows developers to stop this default beh
1 min read
Difference between Event Bubling & Event Capturing ?
Event BubblingEvent bubbling is the default behavior in which an event triggered on a nested element propagates up through its ancestors in the DOM hierarchy, allowing each ancestor to respond to the event. Event Capturing Event capturing is the opposite of event bubbling, where the event is captured on the outermost ancestor first, and then it pro
1 min read
jQuery | Set Content and Attributes
Set Content: It is used to set the content using jQuery. The following methods are used to set the content which are listed below: text(): It is used to set or return the text content of selected elements. html(): It is used to set or return the innerHTML content of the selected element. val(): This parameter is used to set or return the value of a
2 min read
jQuery | Get Content and Attributes
Get Content: In order to get the content of DOM object, there are three simple methods.jQuery methods for DOM manipulation which are listed below: text(): It is used to set or return the text content of selected elements. html(): It is used to set or return the content of selected elements including HTML markup. val(): It is used to set or return t
2 min read
How to conditionally add attributes to React components?
We can conditionally add attributes to React components with the following approaches: Approach 1: Evidently, with some attributes, React is smart enough to omit the attribute if the value you pass to it is not truthy. For example: state= { disabled: false, required: true } return ( &lt;input type="text" disabled={disabled} required={required} /
2 min read
XML | Attributes
Prerequisite: XML | BasicsThe XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.Syntax: &lt;element_name attribute1 attribute2 ... &gt; Contents... &lt;/element_name&gt; In the above syntax element_name is the name of an
3 min read