Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In addition to basic functions for wiring up events in jQuery, there are also a whole bunch of higher-level event handling functions that you can use to handle common situations with events. For example, if I'm here in the API documentation, I'm going to scroll on down to the Events section and click on Mouse Events. And you can see that there are event handlers for things like click, and double-click, hover, mousedown, and mouseup, and so on. These are all a whole bunch of common high-level events that will happen in your web pages, and jQuery provides some nice high- level event handling functions to deal with them.
And so we're going to exercise some of those in this particular lesson. Let's go over to the Example Files folder. Here in the Example Files I've got my helpers_start document, and I'm going to bring that up in the browser. Again, this is pretty much the same example we used in the previous movie. We're going to use the helper functions this time in order to hook up the hover event, along with a couple of other things. Let's go over to the code. All right! So here in the code I've got pretty much the same file as I had before.
This one has the div. It's got the style sheets normal and highlighted, only this time we're going to write the code using the helper functions. So, recall in the previous example I used the on function to bind events for mouseover and mouseleave. However, jQuery gives me a function that already does that. So, I'm just going to write dollar sign, and I'm going to get my evtTarget div. And I'm going to use the hover function. And the hover function basically takes two functions, and those two functions are what you want to happen when the mouse enters something and when the mouse leaves.
In this case, they're both going to be the same function, so I'm going to pass highlight and highlight. Now, I need to write the highlight function, so I'm going to write function highlight. And it's an event handler, so it's going to take an event argument, even though I'm not going to use it. Inside the highlight function I'm going to write the code to get the event target div, and I'm just going to toggle its class like I did last time, and the toggleClass is going to be highlighted.
Okay, that is this class right down here, which will basically change the background color to red. All right! So, I'm going to save, and let's go back to the browser. Let's refresh the file. And now you can see that I'm using the hover function to achieve pretty much the same result as I was achieving using the on function to bind events. Let's go back to the code. Let's use a couple other functions. Let's use a function for, let's see.
We're going to get the evtTarget, and this time we'll use the click function. For the click function what we'll do is we'll call a function called fnClick1. And that's what will happen when the user clicks on it. And let's add another one. Let's do ("#evtTarget").dblclick. This is for handling double-clicks. And we'll just use the fnClick2 function for this.
Now I'm going to write both of those. So, here in the function for fnClick1-- and again that's an event handler, but I'm not going to use the argument. All I'm going to do is simply set the content of the div, by using the HTML function. I'm going to have it say click for the fnClick2 function. I'm just going to copy and paste this. I'm going to write the word Double Click! So, what's happening here is, when the user clicks on the div, this function will get called, and when they double-click on it, this function will get called. So, let's save.
Let's go back to the browser. All right! Let's refresh. There is the hover behavior, and now I'm going to click, and you can see that the click handler causes the content that say Click. Now I'm going to double-click, and you can see that the content changes to Double Click! I can just do that to my heart's content now. So, that's an example of using the higher-level functions to achieve features and effects that you would normally have to use by using the lower-level event binding functions, and there's a whole bunch of these.
I encourage you to check them out.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98399 Viewers
61 Video lessons · 85685 Viewers
71 Video lessons · 69614 Viewers
56 Video lessons · 101939 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.