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

Establishing JavaScript functions

From: HTML5 Projects: Engaging Ecommerce

Video: Establishing JavaScript functions

Time to hook up our range input type to the other elements on the page. First, we'll use a little jQuery magic to add data prices to our paintings, and then we'll insert the necessary JavaScript to filter the paintings by price. So in my code editor, I'm going to scroll down to the bottom and I'm going to add another script tag here. You could combine these if you want, but I'm going to do this just to keep everything a little distinct so it makes it easy to track. And again make a little bit room.

Establishing JavaScript functions

Time to hook up our range input type to the other elements on the page. First, we'll use a little jQuery magic to add data prices to our paintings, and then we'll insert the necessary JavaScript to filter the paintings by price. So in my code editor, I'm going to scroll down to the bottom and I'm going to add another script tag here. You could combine these if you want, but I'm going to do this just to keep everything a little distinct so it makes it easy to track. And again make a little bit room.

Now this time, instead of putting in the code by hand, I actually have a code snippets file that's also found in that same folder, called code snippets.js, that I'm going to go to as a source. And we're going to bring in first this initial code block that you see up op. Now, this is just a simple jQuery declaration that uses a jQuery function to attach data to an element at the DOM level. As you can see, what we're doing here is, for each of the paintings, which have an ID of painting one through six, we're adding in a sequential ID-- just to make it easy to work with--and the individual prices.

So I'm just going to copy this code block and go to index.htm inside my script tag and paste it in. Now that will set up the data prices for all of the paintings. Next, we're going to read them in. Let's go back to my code snippets file, and let's start by walking through the code initially. So after the document is ready, I declare value on line 12 called the value, and then we set up a simple function that monitors the change event of the slider.

And anytime there is a change it gets whatever the value is that's in the Range Value text field and assigns it to our variable, the value, and then it calls another function called Filter Items and passes the value variable. The next code block, starting on line 20, spells out what actually happens in the function filter items. So it takes one argument, which I've called Price Criteria, and that's the same value as the variable the value. And once it has the price criteria, it loops through each of the paintings that are in the gallery and performs a series of functions.

The key one, in line 24, is where it sets the data that's in a particular item to another variable called item data. And now that we have that, we can examine item data to see if the price that's in item data is less than or equal to the price criteria, which you'll recall is the range value. If it does, then we make sure that the opacity is set to 1 and we also set another property called matching to True.

If the price criteria is not less than or equal to the item data price, then we reduce the opacity to .5, dimming it down, and set the itemData.matching to false. Now the itemData.matching I didn't have to include, but I wanted to show you how you could also keep track of things like this by adding in another property. All right, let's copy and paste our code, make sure it's inside the same script tag, paste that in, save it, and now let's head on over to the browser, refresh the page, and I'll scroll down just a little bit here so we can see how the paintings are reacting when I drag the slider. And there you can see that it is in fact starting to reduce the price.

So when I'm at 200, both eye which is 400; arrow, which is 250; and also rabbit, which is 350; are more than the value I've specified in the Price slider, and so they are dimmed. If I bring it back up to 325, I can now afford arrows at 250, but I still can't afford rabbit or eye. Awesome! Our users can now filter the items in the store by price, just by moving the slide.

This technique could be applied to any sort of range-based filtering, such as the number of stars received, number of rooms in the house, whatever you can think of.

Show transcript

This video is part of

Image for HTML5 Projects: Engaging Ecommerce
HTML5 Projects: Engaging Ecommerce

13 video lessons · 6649 viewers

Joseph Lowery
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.

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 HTML5 Projects: Engaging Ecommerce.

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

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.