Trending October 2023 # How Mousedown Event Work In Javascript? # Suggested November 2023 # Top 12 Popular | Phuhoabeautyspa.com

Trending October 2023 # How Mousedown Event Work In Javascript? # Suggested November 2023 # Top 12 Popular

You are reading the article How Mousedown Event Work In Javascript? updated in October 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Mousedown Event Work In Javascript?

Introduction to JavaScript mousedown

JavaScript provides support for various types of events including mouse events. The mousedown is such one kind of mouse event which is supported by JavaScript. The mousedown event can be applied to the element and is triggered whenever the left button of the mouse is pressed over that element. This left button press can be emulated by other types of peripherals such as a touchpad, screen displays, etc and this can also create a mousedown event. When the mousedown event occurs over the element, we can attach a function to execute after it. This event is often used along with the mouseup event where the button is released which is the opposite of mousedown event.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

HTML:

JavaScript:

elementObj.onmousedown = function () {

Using addEventListener() method in JavaScript:

elementObj.addEventListener("onmousedown" , function() { How mousedown Event work in JavaScript?

The mousedown event gets fired immediately the left button is pressed initially.

Examples of JavaScript mousedown

Given below are the examples mentioned:

Example #1

In HTML.

Code:

JavaScript onmousedown Event .body-data { border : #81D4FA 2px solid; background-color : #03a9f400; text-align : left; padding-left : 20px; padding-bottom: 20px; height : auto; width : auto; } .resultText { margin: 0 0 3px 0; padding: 0px; display: block; font-weight: bold; } .heading { font-weight: bold; border-bottom: 2px solid #ddd; font-size: 15px; width: 98%; } function fireEvent( ) { document.getElementById(“result1″).innerHTML = ” Mouse button is pressed “; }

Output:

Before Event occurs:

After Event occurs:

Example #2

In JavaScript.

Code:

JavaScript onmousedown Event .body-data { border : #81D4FA 2px solid; background-color : #03a9f400; text-align : left; padding-left : 20px; padding-bottom: 20px; height : auto; width : auto; } .resultText { margin: 0 0 3px 0; padding: 0px; display: block; font-weight: bold; } .heading { font-weight: bold; border-bottom: 2px solid #ddd; font-size: 15px; width: 98%; } document.getElementById( “myButton” ).onmousedown = function(){ fireEvent(); }; function fireEvent( ) { document.getElementById(“result1″).innerHTML = ” Mouse button is pressed “; }

Output:

Before Event occurs:

After Event occurs:

Example #3

In JavaScript using eventListener() method.

Code:

JavaScript onmousedown Event .body-data { border : #81D4FA 2px solid; background-color : #03a9f400; text-align : left; padding-left : 20px; padding-bottom: 20px; height : auto; width : auto; } .resultText { margin: 0 0 3px 0; padding: 0px; display: block; font-weight: bold; } .heading { font-weight: bold; border-bottom: 2px solid #ddd; font-size: 15px; width: 98%; } document.getElementById( “myButton” ).addEventListener( “mousedown” , function() { fireEvent(); }); function fireEvent( ) { document.getElementById(“result1″).innerHTML = ” Mouse button is pressed “; }

Output:

Before Event occurs:

Example #4

Different operations on different types of elements.

Code:

JavaScript onmousedownEvent .body-data { border : #81D4FA 2px solid; background-color : #03a9f400; text-align : left; padding-left : 20px; padding-bottom: 20px; height : auto; width : auto; } .resultText { margin: 0 0 3px 0; padding: 0px; display: block; font-weight: bold; } .heading { font-weight: bold; border-bottom: 2px solid #ddd; font-size: 15px; width: 98%; } function changeColor() { document.getElementById( “color” ).style.color = “Red”; } document.getElementById( “myText” ).onmousedown = function() { document.getElementById( “myText”).innerHTML = ” Text is changed “; } document.getElementById( “hideText”).addEventListener( “mousedown” , function() { toggleDisplay(); }) function toggleDisplay( ) { var property = document.getElementById( “hideText” ); if ( property.style.display === ‘block’) { property.style.display = ‘none’; } else { property.style.display = ‘block’; } }

Output:

Before Event Occurs:

After Event Occurs:

Conclusion

The onmousedown is a subtype of mouse events that are available in JavaScript. The mousedown event occurs when the left button of the mouse is pressed on the element. It is possible to perform multiple types of operations upon the occurrence of a mousedown event.

Recommended Articles

This is a guide to JavaScript mousedown. Here we discuss how mousedown event work in JavaScript? and examples respectively. You may also have a look at the following articles to learn more –

You're reading How Mousedown Event Work In Javascript?

Update the detailed information about How Mousedown Event Work In Javascript? on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!