JavaScript onmousedown Event
You are Here:
JavaScript onmousedown Event
The onmousedown
event occurs when a user presses a mouse button over an element.
onmousedown Event as Attribute
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p onmousedown="myFunction()">Click me</p>
<script>
function myFunction(){
document.write("You clicked.");
}
</script>
</body>
</html>
Syntax
As Attribute
<element onmousedown = "JavaScript">
As Property
object.onmousedown = function(){
// code
};
Using Event Listener
object.addEventListener("mousedown" , myScript);
onmousedown Event as Property
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Click anywhere</p>
<script>
function myFunction(){
document.write("You clicked.");
}
document.onmousedown = myFunction;
</script>
</body>
</html>
Using addEventListener() Method
Note: The addEventListener() method is not supported in Internet Explorer 8 and below.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Click me</p>
<script>
var x = document.getElementsByTagName("p")[0];
function myFunction(){
document.write("You clicked.");
}
x.addEventListener("mousedown", myFunction);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.