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

Searching JSON data

From: JavaScript and AJAX

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.

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.

Show transcript

This video is part of

Image for JavaScript and AJAX
JavaScript and AJAX

21 video lessons · 21452 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.

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.

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.