Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (68 sloc) 3.77 KB
<?xml version="1.0"?>
<entry type="method" name="mousedown" return="jQuery">
<title>.mousedown()</title>
<desc>Bind an event handler to the "mousedown" JavaScript event, or trigger that event on an element.</desc>
<signature>
<added>1.0</added>
<argument name="handler(eventObject)" type="Function">
<desc>A function to execute each time the event is triggered.</desc>
</argument>
</signature>
<signature>
<added>1.4.3</added>
<argument name="eventData" type="PlainObject" optional="true">
<desc>A map of data that will be passed to the event handler.</desc>
</argument>
<argument name="handler(eventObject)" type="Function">
<desc>A function to execute each time the event is triggered.</desc>
</argument>
</signature>
<signature>
<added>1.0</added>
</signature>
<longdesc>
<p>This method is a shortcut for <code>.bind('mousedown', handler)</code> in the first variation, and <code>.trigger('mousedown')</code> in the second.</p>
<p>The <code>mousedown</code> event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed. Any HTML element can receive this event.</p>
<p>For example, consider the HTML:</p>
<pre><code>&lt;div id="target"&gt;
Click here
&lt;/div&gt;
&lt;div id="other"&gt;
Trigger the handler
&lt;/div&gt;</code></pre>
<p class="image">
<img src="/resources/0042_05_01.png" alt=""/>
</p>
<p>The event handler can be bound to any <code>&lt;div&gt;</code>:</p>
<pre><code>$('#target').mousedown(function() {
alert('Handler for .mousedown() called.');
});</code></pre>
<p>Now if we click on this element, the alert is displayed:</p>
<p>
<span class="output">Handler for .mousedown() called.</span>
</p>
<p>We can also trigger the event when a different element is clicked:</p>
<pre><code>$('#other').click(function() {
$('#target').mousedown();
});</code></pre>
<p>After this code executes, clicks on <span class="output">Trigger the handler</span> will also alert the message.</p>
<p>The <code>mousedown</code> event is sent when any mouse button is clicked. To act only on specific buttons, we can use the event object's <code>which </code>property. Not all browsers support this property (Internet Explorer uses button instead), but jQuery normalizes the property so that it is safe to use in any browser. The value of <code>which</code> will be 1 for the left button, 2 for the middle button, or 3 for the right button.</p>
<p>This event is primarily useful for ensuring that the primary button was used to begin a drag operation; if ignored, strange results can occur when the user attempts to use a context menu. While the middle and right buttons can be detected with these properties, this is not reliable. In Opera and Safari, for example, right mouse button clicks are not detectable by default.</p>
<p>If the user clicks on an element, drags away from it, and releases the button, this is still counted as a <code>mousedown</code> event. This sequence of actions is treated as a "canceling" of the button press in most user interfaces, so it is usually better to use the <code>click</code> event unless we know that the <code>mousedown</code> event is preferable for a particular situation.</p>
</longdesc>
<example>
<desc>Show texts when mouseup and mousedown event triggering.</desc>
<code><![CDATA[
$("p").mouseup(function(){
$(this).append('<span style="color:#F00;">Mouse up.</span>');
}).mousedown(function(){
$(this).append('<span style="color:#00F;">Mouse down.</span>');
});
]]></code>
<html><![CDATA[<p>Press mouse and release here.</p>
]]></html>
</example>
<category slug="events/mouse-events"/>
<category slug="version/1.0"/>
<category slug="version/1.4.3"/>
</entry>
Jump to Line
Something went wrong with that request. Please try again.