New Feature: Playlist Center! Pick a topic and let our playlists guide the way—like a learning mixtape.

Start learning with our library of video tutorials taught by experts. Get started

JavaScript and AJAX
Illustration by

Searching JSON data


From:

JavaScript and AJAX

with Ray Villalobos

Video: Searching JSON data

Previously, we managed to set up our application and load some content from a JSON document. In this video I am going to show you how to handle events from the input field, as well as how to use those events to drive some search functionality into our application. So we can check for an event happening when users interact with a specific element on the page. This technique is called binding the event to the element, and jQuery makes that easy. If you're using straight JavaScript, this is a good list of all the events available in HTML. If you're using jQuery, then you need to check this page for a list of the jQuery version of the same events.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
JavaScript and AJAX
1h 12m Intermediate Jan 18, 2013

Viewers: in countries Watching now:

The ability to read data and communicate information asynchronously is fundamental to modern web applications. In this course, author Ray Villalobos takes a look at integrating AJAX (Asynchronous JavaScript and XML) with JavaScript and jQuery—a combination that makes it easy to work with dynamic data and reduces the number of requests to the server. The course first explains what AJAX is, and shows how to load and reload pages with pure JavaScript, update the DOM, and read and parse data in the XML and JSON formats. Next, learn how to read and write information between the server and the client with AJAX methods in jQuery, and build a dynamic search application that updates search results as you type.

Topics include:
  • What is AJAX?
  • Making requests asynchronous
  • Updating the DOM with getElementById
  • Reading JSON files
  • Working with jQuery and AJAX
  • Sending and searching JSON data
  • Incorporating CSS transitions
Subjects:
Developer Web Programming Languages Web Development
Software:
Ajax JavaScript
Author:
Ray Villalobos

Searching JSON data

Previously, we managed to set up our application and load some content from a JSON document. In this video I am going to show you how to handle events from the input field, as well as how to use those events to drive some search functionality into our application. So we can check for an event happening when users interact with a specific element on the page. This technique is called binding the event to the element, and jQuery makes that easy. If you're using straight JavaScript, this is a good list of all the events available in HTML. If you're using jQuery, then you need to check this page for a list of the jQuery version of the same events.

So we have events like change(), dblclick(), error(), et cetera, et cetera. The one that we're interested in is the keyup() event, an event that happens when somebody releases the key on their keyboard. So let me come over here. I am going to target the input field with the ID of search. Then I am going to bind a keyup element-- this is the jQuery version--and I'll execute a function literal. That function literal will have everything that we did previously. So all the stuff will go right there. Let me indent that a little bit, and I am going to save this.

I will switch over to my page, and I am going to reload the page. Nothing will be showing. As soon as I click right here and start typing something in, then my JSON document will load. This is not really searching anything, but it prevents the display of the JSON data until I type something in the search box. So now it's time to narrow down our results. To do that we're going to need to be able to get the text the user is typing, so we're going to create a variable for that.

Here, once again we target the div with an ID of search, and we're getting the value that is in that field. So let me go ahead and output that to the console. Let me refresh the page and as I type something in here, I should be able to click onto my console. So you can see the text that appears when I type something in the input field down here in the console. That's working pretty well. In order to do as search, we're going to use a JavaScript function called search.

Search takes a string and looks for another piece of text inside that original string. If it finds it, it returns its position. The string method can search for either some text inside another string or use a regular expression. If you search for some text, it will do a case-sensitive search, which is not what we want. So when somebody types a lowercase b, it wouldn't find Barot, because that user has a name that starts with a capital B. And that's not good. Regular expressions are sort of like search on steroids that let you do case-insensitive and more powerful searches.

I am going to need just a very simple regular expression, so let me do that right here. I'll get rid of this console.log, and I'll call this my expression or myExp, and that's going to be a new regular expression. That's going to take in our search field, and it's going to use the case-insensitive search if I type i in quotes right here. Now that we've got these two variables, we can use the search method like this. I am going to wrap this around the list of output items right here, and I will type in if the value of name from our data file and then execute a search passing it a regular expression. The one that we created is called myExp.

If that is not equal to -1, it means that it did find that text in the data. So if it does, then I can output all the stuff. So let me go ahead and save this, and I'll go back into my application and I will reload. I will start typing something in here, and you'll see that it is producing the search result. So let me close this out so you could see it a little better.

There's all my search results right now. It's pretty much everyone. So if I type in a name, like I will type Ferrar here, it finds that second field. I will just do that a little more. It is dynamic, so it's narrowing things down as I type in more and more text. That's pretty good, but right now it's only finding things in the name field. So I also want to check for the Bio field. I can just take this, and I'll add another set of parentheses here and an or symbol.

Paste that other copy of it and change this to check the bio field. So this says look for the regular expressions in the name or in the bio field. I will save that, come over here, we'll refresh, and we'll try something like media, because I know some of these people have the keyword media, or maybe something like art, and now that's working perfectly. So our Live Search functionality is working great, but it doesn't look very hot, so in the next movie I am going to add a little bit of styling.

There are currently no FAQs about JavaScript and AJAX.

 
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.
Upgrade now


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 Upgrade now

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 preferencesfrom 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

Notes cannot be added for locked videos.

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.