• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / jQuery / jQuery mousedown – An Interactive Example of the jQuery mousedown Method

jQuery mousedown – An Interactive Example of the jQuery mousedown Method

May 1, 2022 Leave a Comment

We can use the jQuery mousedown method to run a function when a user clicks down on their mouse. To do this we can add a function call to the mousedown() method.

$("#div1").mousedown(function() {
  //The user has clicked down on their mouse in #div1
});

Another way this can be done is to use the .on() method:

$("#div1").on("mousedown", function() {
  //The user has clicked down on their mouse in #div1
});

We will stick with the first method as we find it to be easier to read.

The jQuery mousedown() method is very similar to the mouseup() method, the main difference being that the mousedown() method will trigger when you click DOWN on the mouse, while the mouseup() method will only trigger AFTER you click AND release the mouse. We will show this in the example below.

An Example of Using the jQuery mousedown Method

In this example, we will have 2 divs set up next to each other. Both divs will be empty. We will add some CSS to float the divs next to each other and display the text in the center.

Here is the HTML setup:


<style>#div1, #div2 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; padding: 20px; } .clear { clear: both; }</style>
<div id="div1">jQuery mousedown - Click down on the mouse</div>
<div id="div2">jQuery mouseup - Click AND release the mouse</div>

When the user moves their mouse into each div and clicks, we will generate a random color and then change the background color of the div.

In the first div, we will be showing how the jQuery mousedown() method works. In the second div, we will show how the mouseup() method works.

Here is the JavaScript code:

//This function below will generate a random color
function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
}
$("#div1").mousedown(function() {
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseup(function() {
  $('#div2').css("background-color", genRandomColor());
});

The final code and output for this example of using the jQuery mousedown() method is below:

Code Output:

jQuery mousedown – Click down on the mouse
jQuery mouseup – Click AND release the mouse

Full Code:


<style>#div1, #div2 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; padding: 20px; } .clear { clear: both; }</style>
<div id="div1">jQuery mousedown - Click down on the mouse</div>
<div id="div2">jQuery mouseup - Click AND release the mouse</div>
<div class="clear"></div>

<script>

function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
}
$("#div1").mousedown(function() {
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseup(function() {
  $('#div2').css("background-color", genRandomColor());
});

</script>

Hopefully this article has been useful to help you understand how to use the jQuery mousedown method.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Change the Font Size of a Paragraph
  • 2.  jQuery width() – Using jQuery to Get the Width of a Div
  • 3.  jQuery insertAfter – Insert Element After Another Element
  • 4.  jQuery next – Get the Next Element of a Div
  • 5.  jQuery focusout – How to Use the focusout() Method on an HTML Form
  • 6.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 7.  jQuery focus – Changing Form Styles with the focus() Method
  • 8.  Using jQuery to Replace HTML Inside Div
  • 9.  Using jQuery to Move Element Before Another
  • 10.  Using jQuery to Select Multiple Ids

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy