Viewers: in countries Watching now:
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.