jQuery bind with 2 live examples (single and multiple events)

What is jQuery bind() method?

The bind() method of jQuery is used to bind or attach one or multiple event handlers to elements. Though from 1.7 version of jQuery, the preferred method to attach event handlers is the  $.on() method.

The bind jQuery method can be used to have the same action for multiple events for the specified element.

Syntax of bind()

Following is the general syntax of using the bing method:

$(selector).bind(event,data,function)

Where:

  • The selector is an element where events to be attached, like a paragraph, or Divs etc.
  • Event =One or more event names like click, mouseenter, mouseleave.  This is required.
  • Data = data to be passed to event handler (optional)
  • Function = is required

Examples of bind() method

Attaching single event

In the example below, a double click (dblclick) event is used to show an alert attached with bind() method to a Div.

jQuery bind

Experience this example online



Attaching multiple events example

To attach multiple events, use the space between event names. In the following example, two events i.e. mouseenter and click are used for single div with the ID=bindtest.

jQuery bind mutiple

Experience this example online



Read also: $.on method of jQuery


Was this article helpful?

Related Articles

Leave A Comment?