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.
$(selector).mousedown(function());
This method accepts only one parameter, which is a function invoked when triggered by the mousedown()
method.
Let’s look at the code below:
In the HTML tab:
div
with id="content"
.jQuery
script.In the JavaScript tab:
mousedown()
method on the element with the id of content ( "#content"
).css()
method to change the background color of that element ( "#content"
) when mousedown()
is invoked.randomColor
that generates a random color and returns it.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.