Attribute | Value | Description |
---|---|---|
onclick | script | Fires on a mouse click on the element |
ondblclick | script | Fires on a mouse double-click on the element |
onmousedown | script | Fires when a mouse button is pressed down on an element |
onmousemove | script | Fires when the mouse pointer is moving while it is over an element |
onmouseout | script | Fires when the mouse pointer moves out of an element |
onmouseover | script | Fires when the mouse pointer moves over an element |
onmouseup | script | Fires when a mouse button is released over an element |
onmousewheel | script | Deprecated. Use the onwheel attribute instead |
onwheel | script | Fires when the mouse wheel rolls up or down over an element |
onclick
event attribute in HTML is used to execute a script, typically a JavaScript function, when a user clicks on an HTML element. It is a fundamental part of creating interactive web pages, allowing developers to define actions that occur in response to user clicks.
Example:
HTML
<button onclick="myFunction()">Click me</button>
The onclick attribute fires on a mouse click on the element.
<element onclick="script">
Value | Description |
---|---|
script | The script to be run on onclick |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
A function is triggered when the button is clicked. The function outputs some text in a p element with id="demo".
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<p>A function is triggered when the button is clicked. The function outputs some text in a p element with id="demo".</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
Click me to change my text color.
A function is triggered when the p element is clicked. The function sets the color of the p element to red.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p id="demo" onclick="myFunction()">Click me to change my text color.</p>
<p>A function is triggered when the p element is clicked. The function sets the color of the p element to red.</p>
<script>
function myFunction() {
document.getElementById("demo").style.color = "red";
}
</script>
</body>
</html>
A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2"><br><br>
<button onclick="myFunction()">Copy Text</button>
<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>
<script>
function myFunction() {
document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>
</body>
</html>
The HTML ondblclick
event attribute specifies the JavaScript code to be executed when a user double-clicks on an HTML element. This attribute is part of the Event Attributes in HTML and can be applied to most visible HTML elements.
Example:
HTML
<button ondblclick="myFunction()">Double-click me</button>
The ondblclick attribute fires on a mouse double-click on the element.
<element ondblclick="script">
Value | Description |
---|---|
script | The script to be run on ondblclick |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
A function is triggered when the button is double-clicked. The function outputs some text in a p element with id="demo".
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<button ondblclick="myFunction()">Double-click me</button>
<p id="demo"></p>
<p>A function is triggered when the button is double-clicked. The function outputs some text in a p element with id="demo".</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
Double-click me to change my text color.
A function is triggered when the p element is double-clicked. The function sets the color of the p element to red.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p id="demo" ondblclick="myFunction()">Double-click me to change my text color.</p>
<p>A function is triggered when the p element is double-clicked. The function sets the color of the p element to red.</p>
<script>
function myFunction() {
document.getElementById("demo").style.color = "red";
}
</script>
</body>
</html>
A function is triggered when the button is double-clicked. The function copies the text from Field1 into Field2.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2"><br><br>
<button ondblclick="myFunction()">Copy Text</button>
<p>A function is triggered when the button is double-clicked. The function copies the text from Field1 into Field2.</p>
<script>
function myFunction() {
document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>
</body>
</html>
onmousedown
attribute in HTML is an event attribute that triggers a script when a mouse button is pressed down on an element. This event is part of the broader category of mouse events in the HTML DOM (Document Object Model).
Example:
HTML
<p onmousedown="mouseDown()">Click the text!</p>
The onmousedown attribute fires when a mouse button is pressed down on the element.
Tip: The order of events related to the onmousedown event (for the left/middle mouse button):
The order of events related to the onmousedown event (for the right mouse button):
<element onmousedown="script">
Value | Description |
---|---|
script | The script to be run on onmousedown |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph. The function sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released. The mouseUp() function sets the color of the text to green.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p id="p1" onmousedown="mouseDown()" onmouseup="mouseUp()">
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph. The function sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released. The mouseUp() function sets the color of the text to green.
</p>
<script>
function mouseDown() {
document.getElementById("p1").style.color = "red";
}
function mouseUp() {
document.getElementById("p1").style.color = "green";
}
</script>
</body>
</html>
onmousemove
event attribute is a global event attribute that triggers a script when the mouse pointer is moved while it is over a specific HTML element.
Example:
HTML
<img onmousemove="bigImg(this)" src="smiley.gif" alt="Smiley">
The onmousemove attribute fires when the pointer is moving while it is over an element.
<element onmousemove="script">
Value | Description |
---|---|
script | The script to be run on onmousemove |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
The function bigImg() is triggered when the user mouse pointer is moved over the image. This function enlarges the image.
The function normalImg() is triggered when the mouse pointer is moved out of the image. That function sets the height and width of the image back to normal.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<img onmousemove="bigImg(this)" onmouseout="normalImg(this)" border="0" src="https://horje.com/avatar.png" alt="Admin" width="32" height="32">
<p>The function bigImg() is triggered when the user mouse pointer is moved over the image. This function enlarges the image.</p>
<p>The function normalImg() is triggered when the mouse pointer is moved out of the image. That function sets the height and width of the image back to normal.</p>
<script>
function bigImg(x) {
x.style.height = "64px";
x.style.width = "64px";
}
function normalImg(x) {
x.style.height = "32px";
x.style.width = "32px";
}
</script>
</body>
</html>