Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with jQuery and AJAX

From: JavaScript and AJAX

Video: Working with jQuery and AJAX

jQuery is a great library and one of its mottos is that you can write less and do more, and one of the places where you can really see that is when working with AJAX. So let's take a look at how we can use AJAX with jQuery. jQuery provides several methods for working with AJAX calls, and you can see all them in this page right here. So for the most part, you end up working with one or two of these. So let's go ahead and load some content onto our page. Our HTML file is a simpler version of what we had in the last chapter, just a div with an id of update, and we're loading the jQuery as well as our script file.

Working with jQuery and AJAX

jQuery is a great library and one of its mottos is that you can write less and do more, and one of the places where you can really see that is when working with AJAX. So let's take a look at how we can use AJAX with jQuery. jQuery provides several methods for working with AJAX calls, and you can see all them in this page right here. So for the most part, you end up working with one or two of these. So let's go ahead and load some content onto our page. Our HTML file is a simpler version of what we had in the last chapter, just a div with an id of update, and we're loading the jQuery as well as our script file.

The script document right now is empty, and our data file just has the words Hello World. So I'm going to switch over to my script and I'm going to type in some jQuery right here. So I'm going to target the update id element and then load into it our data.txt document. So let me save that and switch over to my browser, refresh my document, and you can see the text Hello World. So there's actually a lot going on in that very simple line of jQuery code. jQuery is taking care of a lot of problems with backwards compatibility in older browsers, and it also creates and sends the XHR request.

Not only that, jQuery gives you new ways of selecting elements. Here we're selecting something in the DOM-- in this case a div with an id of update. If you watched the chapter on modifying the DOM, you know that JavaScript has two main methods for getting to elements: getElementById and getElementsByTagName. You may have been wondering why I didn't talk about getElementsByClassName, and that's because although there's some support for this method in a lot of modern browsers, the support is not yet universal, and even if it were, working with complex selectors is much easier to do with jQuery.

Let's modify our code so that we can use a class name instead of an ID. So I'm just going to change this ID right here to class, and all I have to do in jQuery is just modify this pound sign right here to select the class of update. I'm going to save that, switch over to my browser, refresh, and the text still comes up. So, classes are supposed to refer to elements that occur multiple times in the DOM. Let's go ahead and go to the HTML file and just copy this update element a few times. So I'm going to save this and I'll go back to my browser and I'll hit refresh, and now you can see that it's loading multiple times.

So the load event is just going to take all our content with the same class and replace it with the text from our file. So jQuery is not only going to improve what you can do with JavaScript, but it goes beyond that and gives you totally new selectors with which to access the data. So check this out. I'm going to go into my JavaScript file. So what I'm going to do is change this so that I only select the first of all the update classes. So I'm going to save that and switch over to my browser and I'm going to refresh. And you can't really see what's going on here. So I'm going to over to the Elements. I have the Developer tools open.

If you don't have that open, just right-click and select Inspect Element. I'm using Google Chrome as my browser. But take a look at the code right here. It's still showing the other classes, but it's only changing the first one. And that's pretty cool. So let's go back and try another one. We can actually tell jQuery to just load up the even classes. And I'll save that, come over here, refresh, and if you take a look at our code, we've got three up here, but it only did every other class right here--and that's really, really powerful, easy way to do something like zebra striping on tables.

Using jQuery is going to take care of a lot of the grunt work necessary to work with AJAX. It also gives you some powerful new ways to select DOM elements. I think it's good to understand the XHR object and how JavaScript handles things. When it comes down to taking care of business, it's hard to argue with the convenience that JQuery provides. If you want to know more by jQuery, make sure you take a look at Joe Marini's excellent course, jQuery Essential Training.

Show transcript

This video is part of

Image for JavaScript and AJAX
JavaScript and AJAX

21 video lessons · 19768 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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

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 JavaScript and AJAX.

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


OK
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 ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.