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

HTML5 Projects: Engaging Ecommerce

Establishing JavaScript functions


From:

HTML5 Projects: Engaging Ecommerce

with Joseph Lowery

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.

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...
HTML5 Projects: Engaging Ecommerce
37m 29s Intermediate Nov 06, 2012

Viewers: in countries Watching now:

The HTML5 Projects series combines HTML5, CSS3, and recent JavaScript API technologies to work—enhancing your web projects with interactivity and multimedia. This installment shows you how to build features that allow visitors to your online store to filter their selections interactively via sliders, drag items onto wish lists, and automatically save them locally. Author Joseph Lowery uses this scenario as an opportunity to introduce concepts such as HTML5 form elements with JavaScript enhancements, local content storage, and drag-and-drop.

Topics include:
  • Including range form elements
  • Modifying the display interactively
  • Establishing JavaScript functions
  • Dragging items to wish lists
  • Storing wish list items locally
  • Saving and retrieving list items
Subjects:
Web User Experience Ecommerce Web Design Projects
Software:
HTML JavaScript CSS
Author:
Joseph Lowery

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.

There are currently no FAQs about HTML5 Projects: Engaging Ecommerce.

Share a link to this course
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

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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