Events triggered by actions inside a HTML form (applies to almost all HTML elements, but is most used in form elements):
Attribute | Value | Description |
---|---|---|
onblur | script | Fires the moment that the element loses focus |
onchange | script | Fires the moment when the value of the element is changed |
oncontextmenu | script | Script to be run when a context menu is triggered |
onfocus | script | Fires the moment when the element gets focus |
oninput | script | Script to be run when an element gets user input |
oninvalid | script | Script to be run when an element is invalid |
onreset | script | Fires when the Reset button in a form is clicked |
onsearch | script | Fires when the user writes something in a search field (for <input="search">) |
onselect | script | Fires after some text has been selected in an element |
onsubmit | script | Fires when a form is submitted |
The HTML onblur
event attribute specifies a script to be executed when an element loses focus. This event is triggered when a user moves away from an element that currently has focus, such as clicking outside of a text input field or navigating to another element using the Tab key .
Example:
HTML
<input type="text" name="fname" id="fname" onblur="myFunction()">
The onblur attribute fires the moment that the element loses focus.
Onblur is most often used with form validation code (e.g. when the user leaves a form field).
Tip: The onblur attribute is the opposite of the onfocus attribute.
<element onblur="script">
Value | Description |
---|---|
script | The script to be run on onblur |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
When you leave the input field, a function is triggered which transforms the input text to upper case.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" name="fname" id="fname" onblur="myFunction()">
<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
<script>
function myFunction() {
let x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
When you enter the input field, a function is triggered which sets the background color to yellow. When you leave the input field, a function is triggered which sets the background color to red.
Example:
HTML
<input type="text" onfocus="focusFunction()" onblur="blurFunction()">
onchange
event attribute in HTML triggers a script when the value of an element is altered and the element subsequently loses focus. This attribute is commonly used with form elements such as <input>
, <select>
, and <textarea>
.
Here executes a JavaScript when a user changes the selected option of a <select> element.
Example:
HTML
<select onchange="myFunction()">
The onchange attribute fires the moment when the value of the element is changed.
Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus. The other difference is that the onchange event also works on <select> elements.
<element onchange="script">
Value | Description |
---|---|
script | The script to be run on onchange |
Supported HTML tags: | <input type="checkbox">, <input type="file">, <input type="password">, <input type="radio">, <input type="range">, <input type="search">, <input type="text">, <select> and <textarea> |
---|
When you select a new car, a function is triggered which outputs the value of the selected car.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p>Select a new car from the list.</p>
<select id="mySelect" onchange="myFunction()">
<option value="Audi">Audi
<option value="BMW">BMW
<option value="Mercedes">Mercedes
<option value="Volvo">Volvo
</select>
<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>
<p id="demo"></p>
<script>
function myFunction() {
let x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
Modify the text in the input field, then click outside the field to fire the onchange event
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p>Modify the text in the input field, then click outside the field to fire the onchange event.</p>
Enter some text: <input type="text" name="txt" value="Hello" onchange="myFunction(this.value)">
<script>
function myFunction(val) {
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
The oncontextmenu
event attribute in HTML is used to specify a JavaScript function or script to be executed when the user right-clicks on an element, triggering the context menu. This event allows developers to customize or prevent the default browser context menu behavior
Example:
HTML
<div oncontextmenu="myFunction()" contextmenu="mymenu">
The oncontextmenu attribute fires when the user right-clicks on an element to open the context menu.
Note: Although the oncontextmenu event is supported in all browsers, the contextmenu attribute is currently only supported in Firefox.
<element oncontextmenu="script">
<element oncontextmenu="script">
<element oncontextmenu="script">