What is the mouseup() method in jQuery?

Overview

The mouseup() method triggers the mouseup event when the left mouse button is pressed and released over a selected element. It is a short-hand of the on('mouseup', eventHandler) event.

Syntax

$(selector).mouseup(function())

Parameters

This method accepts only one parameter, which is a function invoked when triggered by the mouseup() method.

Example

Let’s look at the code below to understand how it works.

Implementation of mouseup() method

Explanation

HTML

  • Line 6: We create a div with id="content".
  • Line 7: We import the jQuery script.

JavaScript

  • Line 4: We attach the mouseup() 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") when mouseup() is invoked.
  • Lines 10–15: We create a function randomColor, which generates a random color and returns it.

Output

The mouseup() method is invoked once we press and release the left mouse button over the square. As a result, the background color of the square changes.

Free Resources