home

Jquery bind() Method

Event bind() method adds to matched or selected elements. here we have example about bind()method, this method when the uses clicks on button binds the click event to button(with id of #clickMe) then alert will of text.

Jquery bind() Syntax

$(selector).bind(eventType,eventData,function,eventmap)

Single Event Binding code

<script>
 //html code button code
<button id="clickMe">Click me</button>

//jquery bind() method code
$(document).ready(function(){
  $("#clickMe").bind( "click", function() {
  alert( "Hay Hello! Clicked on button" );
});
});
</script>

Multiple events binding code

Multiple events can bound by using space to separate. we have couple of events (mouseenter and mouseleave), The effect of this on <div class="main"> (when it does not initially have the " temporary " class) is add the temporary class when the mouse enter the div and remove when the mouse leaves.

<script>
//html code
<div class="main">simple div tag<div>

//Jquery
$( ".main" ).bind( "mouseenter mouseleave", function() {
  $( this ).toggleClass( "temporary" );
});
</script>