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

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 · 6470 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

Are you sure you want to delete this note?

No

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.