Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Triggering a change based on activity with event binding


From:

jQuery for Web Designers

with Joe Chellman

Video: Triggering a change based on activity with event binding

When something happens on a page like I click a field or I type in it or I change a value somewhere, an event is created, which means that I have an opportunity in my JavaScript code or jQuery to do something. And in this movie, we're going to take a look at how that works. So here I am in the file that makes this page. Let's scroll down to my code block, and I have a little bit of placeholder code in here already. So if you've ever dealt with events in JavaScript before you know that especially if you have to support older versions of IE it can get a little hairy sometimes. JQuery makes it very easy.
Expand all | Collapse all
  1. 2m 17s
    1. Welcome
      1m 4s
    2. What you should know
      37s
    3. Using the exercise files
      36s
  2. 11m 59s
    1. jQuery is a JavaScript library?
      1m 15s
    2. When to use jQuery
      2m 51s
    3. Alternatives to jQuery
      1m 24s
    4. Which version of jQuery to use
      1m 50s
    5. How to install jQuery
      3m 21s
    6. Reference materials
      1m 18s
  3. 19m 19s
    1. Getting ready
      2m 26s
    2. Selecting elements to use
      3m 54s
    3. Performing multiple operations on the same line with chaining
      2m 30s
    4. Using classes to find what you're looking for
      3m 52s
    5. Adding, modifying, and removing content dynamically
      4m 3s
    6. Challenge: Form feedback
      1m 12s
    7. Solution: Form feedback
      1m 22s
  4. 18m 28s
    1. Triggering a change based on activity with event binding
      4m 37s
    2. Reading and changing values
      4m 17s
    3. Working with HTML attributes
      4m 55s
    4. Challenge: Dynamic contact form
      1m 27s
    5. Solution: Dynamic contact form
      3m 12s
  5. 16m 42s
    1. Using the Colorbox plugin to build a slideshow gallery
      4m 22s
    2. Implementing Colorbox on your site
      2m 46s
    3. Changing Colorbox options
      5m 53s
    4. Challenge: Convert to a slideshow
      1m 6s
    5. Solution: Convert to a slideshow
      2m 35s
  6. 28m 37s
    1. Using jQuery or CSS to animate elements
      2m 24s
    2. Creating simple jQuery animations
      4m 35s
    3. Animating numeric properties with animate()
      4m 56s
    4. Understanding animation easing
      4m 4s
    5. Putting it together: Flowers in the cart
      6m 4s
    6. Callbacks: What to do when the animation ends
      3m 27s
    7. Challenge: Improve the animation
      1m 3s
    8. Solution: Improve the animation
      2m 4s
  7. 4m 45s
    1. More fun plugins
      2m 38s
    2. What's next: More jQuery
      50s
    3. What's next: More JavaScript
      1m 17s

please wait ...
Watch the Online Video Course jQuery for Web Designers
1h 42m Intermediate Oct 31, 2013

Viewers: in countries Watching now:

Take the next step in your web design career with jQuery, which amplifies JavaScript's power and puts a library of prebuilt functions and a diverse selection of plugins at web designers' fingertips. This course explains what jQuery is, how to install it, and use it to script more interesting, interactive websites. Author Joe Chellman will show you how to use jQuery to add web form usability, audio and video, animation, and other features like slideshow galleries to your existing HTML and CSS-based webpages.

Check out JavaScript for Web Designers for more detailed instruction on JavaScript.

Topics include:
  • What is jQuery?
  • Installing jQuery
  • Performing multiple operations with chaining
  • Using classes
  • Adding, modifying, and removing content dynamically
  • Triggering a change with event binding
  • Creating a dynamic contact form
  • Building a slideshow gallery
  • Creating simple jQuery animations
Subject:
Web
Software:
jQuery
Author:
Joe Chellman

Triggering a change based on activity with event binding

When something happens on a page like I click a field or I type in it or I change a value somewhere, an event is created, which means that I have an opportunity in my JavaScript code or jQuery to do something. And in this movie, we're going to take a look at how that works. So here I am in the file that makes this page. Let's scroll down to my code block, and I have a little bit of placeholder code in here already. So if you've ever dealt with events in JavaScript before you know that especially if you have to support older versions of IE it can get a little hairy sometimes. JQuery makes it very easy.

So I'm going to uncomment a portion of this. There are many different events that you might respond to or listen for. There's click events, key up events for when somebody's typing in a field, change events for when the value of a field changes. And JQuery makes it easy to respond to all of them, using the On method. So the way this works is I select my element that I want to listen for an event. And then I use On and pass it a couple of arguments. First is the name of the event that I want it to listen for. In this case we're going to listen for a click on this name field. And then I give it a handler function.

In this case I'm using a named function called nameClick. And then I'm going to do a couple of things. So, the one that we're going to look at first, even though a couple of things are happening, but the one we're going to look at first is just logging a message to the console. I'm going to comment these out while we look at this and save this. Now I'll switch back over to my browser, and I will open up the JavaScript console with cmd+opt+i and reload this page. Now, if I scroll down to this name field and click. You can see that we logged that message just like we meant to. Now, let's take a look at these side by side.

So there's a lot of different ways that you can phrase your event handlers in jQuery. I can use On with a named function. I can also take this function, and instead of making it a named function, I can turn it into an anonymous function, copy the whole thing and replace that name click like this. And if I'm just adding one function for this one event handler and I'm not reusing it. This is a nice way to keep things a little more compact. Also, instead of using On, jQuery offers dedicated methods for pretty much every major event type. So instead of using On click like this, I could just use click and then pass it the event handler directly.

All of these things work and I'm pointing this out because, if you're reading other people's jQuery code, the way that these things are done has kind of evolved over time and so you might see different ways of doing this. I'm going to stick with On for now. Now when I switch back over here I'm using an anonymous function but it still works. Great. Here's another important point when you're working with events. Often inside your event handler you're going to want to make reference to that which was clicked. If you've used JavaScript before you may recognize the keyword this which is often used for exactly this purpose. So when I click something, the keyword this is automatically assigned to the thing that I clicked.

And then I'm using the jQuery selector around this to give me the ability to do jQuery stuff to this. So if I uncomment this next line I'm now taking this and setting its background color. So I've saved this file, go back over hear and re-load. And there we go. I clicked the field, I get my message and the field turns pink. So this is probably the best way to work with something using more jQuery inside of your event handler. I want to show you a couple of other kinds of events that we've already mentioned but you haven't seen in action. So I can change this click event to key up and I'll change the message so it says you typed in the name field.

Get rid of everything else and save it. Now every time I type something a message gets logged. If you want to respond to keys in a text field, like if you were implementing a number of character counter or something like that somewhere on your page, this is the event that you would listen for. Likewise if I want to respond when the value of something changes, I can use the change event. Save this, reload, hey hey hey. Tab away from the field and that fires the change event on that name field. And so of course there are many, many events available to respond to. These are just a few of the most common ones. And then one more thing I'd like to mention is the submit event. This is something that every forum has.

And in this case I'm listening for the submit event and here's my anonymous function that handles that event. To which I'm passing this argument event. When you listen for an event, you can pass in an argument. You can it anything you want. Some people will just call it E, but whatever it is, it refers to the triggered event. And then you can use this method called, prevent default on that event. Which stops any default actions from happening. So if I comment this out and we just let this thing submit normally and reload, we go to the submission page.

And I'll go back, but now if I activate this prevent default and reload, when I submit this form nothing happens. And sometimes you'll want to do that if you're working with form validation or the like. So that's an overview of some of the different events that are in jQuery and how you can start to respond to them.

There are currently no FAQs about jQuery for Web Designers.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed jQuery for Web Designers.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.