What is the mousedown() method in jQuery?
Overview
The mousedown() method triggers the mousedown event when the mouse pointer is over a selected element and the mouse button is pressed. It is a short-hand of the on('mousedown', eventHandler) event.
Syntax
$(selector).mousedown(function());
Parameters
This method accepts only one parameter, which is a function invoked when triggered by the mousedown() method.
Code example
Let’s look at the code below:
Code explanation
In the HTML tab:
- Line 6: We create a
divwithid="content". - Line 7: We import the
jQueryscript.
In the JavaScript tab:
- Line 4: We attach the
mousedown()method on the element with the id of content ("#content"). - Line 6: We use the
css()method to change the background color of that element ("#content") whenmousedown()is invoked. - Lines 10 to 15: We create a function,
randomColorthat generates a random color and returns it.
Output
The mousedown() method is invoked once we move the mouse pointer over the square and press the mouse button. As a result, the background color of the square changes.