Handling Client Events in jQuery
Background:
We already discussed a few things about jQuery. Please go though my earlier posts for the basics of jQuery, jQuery selectors and Manipulating DOM using jQuery. Events are another very important aspect of jQuery. In this post we are going to discuss a few things on jQuery events.Introduction:
In JavaScript there are events and we can write code snippet against those to perform certain action. Events are basically the response from the web page based on certain user action. There are defined event attributes for each html elements and we can use those to create our own jQuery code.
Like the click event there are so many events which we can use to define the response of the web page against different user action. Following are a fewcommon events which we can use:
In order to do this, we have the unbind() event. We can unbind selective events from the selected elements or may choose to even remove all event from the selected elements.
Let’s look at the below example:
Events:
We can always call a jQuery function from one of the events of an element, and In order to do that, we can simply mention the function name in the HTML attribute of that element as shown below
<ahref="#"onclick="alert('Hello,
world!');">Click</a>
<ahref="#"onclick="showHelloWorld()">Click</a>
In the above code snippet, we are writing jQuery code and that code snippet is associated by mentioning that with the onclick attribute of the element.
But, what if we want to associate a single jQuery method to be called from multiple element’s click event? Calling the onclick function in the onclick attribute will work but that is not the samertest way to do that.
The approach for this will be to bind the jQuery method with an event of selected elements. As we have already gone through different selectors in jQuery we can use that technique for this.
Let’s look at the below example:
But, what if we want to associate a single jQuery method to be called from multiple element’s click event? Calling the onclick function in the onclick attribute will work but that is not the samertest way to do that.
The approach for this will be to bind the jQuery method with an event of selected elements. As we have already gone through different selectors in jQuery we can use that technique for this.
Let’s look at the below example:
$("div.row").bind("click", function(){
alert(“Hello WOrld”);
});
In the above example, we are binding an anonymous function with the click event of all the “div” element which have a class “row”. We can write the above code snipper in the $.ready() event of the page to get the desired effect.
Following are different variation of doing the same thing as discussed above:
Following are different variation of doing the same thing as discussed above:
$("div.row").on("click", function(){
alert("Hello World");
});
$("div.row").click(function(){
alert("Hello World");
});
Like the click event there are so many events which we can use to define the response of the web page against different user action. Following are a fewcommon events which we can use:
- click
- dblclick
- mouseenter
- mouseleave
- keypress
- keydown
- keyup
- submit
- load
- change
- resize
- focus
- scroll
- blur
- unload
Unbinding events from control:
We have already seen the way to bind a jQuery method with an event of html element. This enables to actually execute the jQuery method wherever that event occurs. In other words this enables the event to subscribe to that event. jQuery allows us to remove this subscription because that can be needed.In order to do this, we have the unbind() event. We can unbind selective events from the selected elements or may choose to even remove all event from the selected elements.
Let’s look at the below example:
- $("a").unbind(); // unbinds any event that are attached using the bind event
- $("a").unbind("click dblclick"); un-attache the click and dblclick event
Apart from the events we discussed there are many more events which we can use to customise the webpage response to user interactions. The complete list of jQuery events are here
Chaining in jQuery:
In real world applications we may need to perform multiple action, events on a single element. In order to do that, if we select the same element again and again and perform one action at a time on that, then that is definitely not a smart way to do things, we want to select the element once and perform multiple action/attach multiple events on that. One really interesting aspect of jQuery is most of the methods that jQuery provides, return jQuery object. So we can chain multiple method on selected element.Let’s look at the Following Example:
$("#Div1").bind("click",function(){
alert("Div1 Clicked");
})
.text("Hello,
world!")
.removeClass("blue")
.addClass("bold")
.css("color", "blue")
.slideUp(2000)
.slideDown(2000);
Comments
Post a Comment