navigate site menu

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

HTML5 Projects: Engaging Ecommerce

HTML5 Projects: Engaging Ecommerce

with Joseph Lowery

 


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

show more

author
Joseph Lowery
subject
Web, User Experience, Ecommerce, Web Design, Projects
software
HTML , JavaScript , CSS
level
Intermediate
duration
37m 29s
released
Nov 06, 2012

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(music playing)
00:04Hi! I am Joe Lowery. I am so happy you've joined me for HTML5 + UX:
00:10Engaging Ecommerce.
00:12First, I'll show you how to use the coolest of the many new form elements in
00:16HTML5, the range input type, to filter catalog items interactively.
00:23Next, we'll bring drag-and-drop into play, so site visitors can pick their
00:28favorite products quickly and with a little flair.
00:31Finally, I'll demonstrate how to automatically store selected favorites for the
00:36user via the cutting-edge JavaScript method known as local storage.
00:41As you go through the course keep in mind that the ecommerce catalog is just
00:46one example of how HTML5 can greatly enhance your user's experience.
00:51All right, let's get started.
Collapse this transcript
About the exercise files
00:00I am very happy to announce that any member of the lynda online training library--
00:05whether premium, monthly, or annual-- has access to the exercise files used
00:10throughout this title.
00:12Naturally, the information in this course is ultimately intended to be applied
00:16to your own web pages.
00:18However, you might find it helpful to work with the supplied exercise files to
00:22practice the steps safely and without creating your own examples.
00:26To begin using the files from the site, download them, extract them, and then
00:30store those files in a convenient location, such as your desktop.
00:34The Exercise Files folder is organized by chapter, and chapters are broken down
00:39into the various lessons that you'll see.
00:42Within the lesson chapters there are a series of files, typically HTML, CSS,
00:46images, and so forth, that make up the practice file.
00:50For most lessons you'll also find a folder named final, and that contains files
00:56showing the completed lesson, which you can open up and explore at your leisure.
01:00You are free to either follow the whole course chapter by chapter or jump in at any point.
01:05If you'd rather not use the exercise files, you can definitely follow along
01:09with your own assets.
Collapse this transcript
Course requirements
00:00To follow along with this course the requirements are pretty straightforward.
00:04From a tools perspective, you'll just need a code editor and a number of
00:08browsers for testing.
00:09You can use whatever code editor you prefer.
00:12I am going to be working with Aptana on the Mac, a free editor for both Mac and Windows.
00:18Most of our browser demonstrations and testing will be using a pretty current
00:22version of standards-based browsers like Google Chrome.
00:25As the focus in this course is on some of the most recently released
00:29technologies, I'll point out, or you might run into a problem, working with
00:34more recent browsers.
00:36You probably shouldn't depend on the techniques demonstrated if your client
00:40needs to support legacy browsers like Internet Explorer 6.
00:43Well, that's it for tools.
00:46From a knowledge perspective, you should be pretty familiar with both HTML and
00:50CSS, with a general understanding of JavaScript.
00:54I'll walk through any advanced JavaScript we use block by block, so you can
00:58understand what's going on under the hood.
01:01One last thing: the absolute best thing you can bring to this course is your imagination.
01:07Although I'll be going through all the details of applying HTML5 technologies to
01:11a specific project, you'll get the most benefit if you keep your mind open for
01:16other ways you can apply the same lessons to your own work.
Collapse this transcript
1. Understanding the Project
Examining what we're going to build
00:00Let's take a quick spin through the complete project so you can get a full
00:04picture of what's ahead.
00:06If you are the type who likes to dive right into the code and you have access
00:10to the exercise files, you'll find the completed project in the Chapter 1 01_01 folder.
00:17Here we are, in the Roux Academy Store, where I have six paintings for sale.
00:23As you can see, they range in price from $400 for the eye to $75 for graffiti.
00:32One of the top considerations for any user in any online store is obviously
00:38price, but how to narrow down that price?
00:41Well, over here on the right we have a Price slider.
00:47When I drag this slider to the left to indicate a lower price, watch what happens.
00:55Paintings that are more expensive, out of my price range, are dimmed, like the eye and the rabbit there.
01:02So as you can see, the Price slider is very visual and it's very easy to
01:06understand, which are key aspects of an enhanced user experience.
01:11Once the shopper has narrowed his or her options, we want to make it easy for
01:16them to note their favorites. As this big text indicates here under the Favorites
01:21heading, all the user has to do is to drag their favorites to the right.
01:26So let me scroll down just a little bit so we see all of our paintings.
01:30And let's say I'm kind of interested in orange star here, so I'll place my
01:34cursor over it and with the arrows indicating there that I can drag something,
01:39click and drag it over. When I move it over into the target area, a plus sign
01:43appears, and if I drop it, it's listed under my Favorites.
01:47Let's do that again with Graffiti, just so you can see how the list is built up.
01:51You'll notice that when I come into the drop zone I get an indication that I've
01:55entered that area by the change of the background color, and I'll drop it, it
02:00changes back, and now Graffiti is on my list of favorites.
02:04But what if they don't purchase right away?
02:06We'll make it painless by automatically saving their favorites so that the next
02:11time they visit the site their favorites will be waiting for them.
02:14Let me simulate that experience by closing the browser window and then I'm going
02:21to open a new window and go to my history, and if I scroll down now, you'll see
02:28that the favorites are waiting for me.
02:30Now that you've seen an overview of what we're doing, you're ready to dive a
02:34little deeper into how we're doing it, with an explanation of the related
02:38technologies in the next lesson.
Collapse this transcript
Exploring highlighted HTML5, CSS3, and JavaScript technologies
00:00Before we get into actually building a project, I want to take a few moments to
00:04talk about each of the key technologies we are using.
00:07In all, we're going to be targeting three different technologies: the range form
00:12element, drag and drop, and localStorage.
00:15Let's start with the form element range.
00:18Here is the basic code for a range form element.
00:23As you can see, it's an input tag where the type is set to range. And there are
00:29several other attributes that you need to include.
00:33Minimum is now set to 0 in our example, maximum I have set at a 100, and
00:39then value is set to 50.
00:41Wherever you set the value is where the slider handle will appear to be on the range control.
00:48Now, there is also an optional attribute called step.
00:52Once you declared the step value, your range control will move in those increments.
00:57There is decent browser support for the range form element.
01:00Current versions of Chrome, Safari, and Opera already supporting it, and soon
01:05Internet Explorer 10.
01:08The only major holdout is Firefox, which currently displays just a text field
01:14which the user can enter a value into if they choose.
01:17However, there is no interactive slider that they can move back and forth.
01:21One hopeful sign: I have noted in the Firefox development circles that they do
01:26seem to be working on this feature for some time in the future.
01:30If you are unsure of what browsers are supported at what level, I heartily
01:34recommend going to caniuse.com, and once you are there, just search for range.
01:41Let me show you what the range input type looks like in Chrome.
01:47As you can see, here is that same code that we had: the range from 0 to 100
01:54that starts off at 50.
01:56Now, it's pretty obvious that there is one big limitation to the current
02:00specification and browser implementation, and that is there is no associated
02:06value field to indicate what the slider is representing.
02:10It is, however, relatively easy to implement, as you'll see during our project, and
02:14I do want to point out there is at least one other ready-made option, which you
02:18can find on the jquerytools.org site.
02:22Next up, let's take a look at drag and drop.
02:25Drag and drop has two different aspects to it: one minor but critical one on the
02:30HTML5 side and a much more robust series of options with the JavaScript API.
02:35On the HTML5 side all you need to do is to put in draggable=true into any
02:43object that you want to be able to drag.
02:46With JavaScript there are seven different events, covering everything
02:50from dragstart to drop.
02:53There is one key related object, and that's the dataTransfer object.
02:58This is used for, as the name implies, transferring information from a dragged
03:04object to a dropped one.
03:06So you set the data transfer object and the drag start event and then it's
03:11read in the drop event.
03:13The third technology used in this project is localStorage.
03:17localStorage is really very straightforward to use, and it adds a state to
03:22websites without delving into server-side coding.
03:26Unlike with cookies, which is another way web developers have handled the state
03:30issue, local storage is handled through the browser cache.
03:34Significantly, it also has a much larger limit, as opposed to cookies.
03:39It's basically a name/value pair structure, and while there are ways around it--
03:44primarily using JSON--you're normally storing and retrieving strings.
03:50There are essentially two simple commands. First localStorage.setItem.
03:54Here the first of the two arguments is the name of the item to be stored and the
04:00second is its value.
04:02The second command, getItem, just uses one argument, and that's of course the name
04:07of the item you want to retrieve.
04:09Browser support for local storage is really rocking.
04:12You can find all the details on caniuse.com, under local storage.
04:17As you can see, it's been supported for quite some time, including all the way
04:21back to Internet Explorer 8.
04:23So those are the tools we get to play with as we enhance the user experience
04:28for an ecommerce catalog, starting in the next chapter.
Collapse this transcript
2. Filtering with Sliders
Including range form elements
00:00The first step towards enhancing the user experience of our ecommerce catalog
00:05is to add in a form element new to HTML5, the range input type.
00:10It's a very straightforward tag with just a few attributes.
00:14In this lesson I'll also add in another form element,
00:18a basic text input type, so we can eventually display the actual range value
00:23as the slider moves.
00:25Finally, I'll show you a very cool CSS property that you don't want to implement
00:31to help with your slider stylings.
00:33So here's what the page looks like at this current stage.
00:36You can see over on the right, under the Price Slider area,
00:40there is nothing there.
00:41So now let's switch over to our code editor.
00:44So I am going to scroll down to about line 67 here, where I have a little handy
00:50placeholder, insert range input type here.
00:54Let's delete that, and now I'll put in my code for the input tag, starting with input.
01:01I'm going to enter an id that we'll use later in our JavaScript routines, and
01:06let's just call this slider.
01:09Next, we'll enter in the type for this input tag, and the type is going to be of course range.
01:17We want to enter in a number of different attributes now.
01:20Let's put in our minimum first. And I'm going to set the min to 50.
01:26We don't want to make our artwork too cheap. And we also made a maximum.
01:30So max, and let's make that 400 or too expensive.
01:35This is a school after all.
01:37Next, I'm going to put in an optional argument, step, so we can change how the
01:42range increments, and let's put in a step value of 25.
01:47Finally, we want to put in our starting point, and that's handled through
01:50the value attribute.
01:51In some situations, as in the example I gave in an earlier chapter, you might
01:57want it to start right dead center, but in this situation, since what we're
02:01going to be doing is filtering out less expensive items, we want to show all of
02:05the items to start with.
02:06So we're going to place this value at its maximum, the upper limit, 400.
02:10Okay, let's close off the input tag. Now that's it for the slider, but as you saw
02:17in the previous chapter, there is an issue with the implementation of the slider
02:21where there is no real way to show what the range value is.
02:26So let's put in another tag.
02:29This will be a simple text field, also an input tag, and this will have
02:35an id=rangeValue.
02:39That will again help us identify it for JavaScript routines a little
02:43further down the line.
02:46Next we'll set the type and set that to text. And I'm just going to put in a
02:50minimum size, and we'll make it size=2. And then let's close off the tag, and that's done.
02:58So I'm going to save it. Let's head back to the browser for a second,
03:02refresh the page, and there is our basic slider and the accompanying text field.
03:07Let's bring in some styles to spice it up a bit.
03:10I already have the styles created, so all we need to do is to link them in.
03:15I am going to move up to the head section of the page, and we'll put this right
03:20after our link to the main.css file. So it's link href=, and we'll copy the path,
03:31CSS main. As you can see, the code hints begin to come up, so let's take
03:35advantage of that and we're going to go to range.css, and it's also going to be
03:41rel=stylesheet and we'll use the same media tags: screen and projection.
03:52Okay, we close that tag, and I'll save the file.
03:55Now let me open up that particular CSS file, and you can see it has two rules in it:
04:04one that targets the input with a type of range and then the other one that
04:09goes a little bit further. That's actually targeting the thumb control that moves the sliders.
04:15Now most of the properties that you see here, from the second one in each entry
04:20on down, are pretty normal, and there is no reason to spend any time on them.
04:24However, the very first entry in both rules, -webkit-appearance, deserves a
04:29little bit of explanation.
04:31This property allows you to specify a uniform look and feel for all your widgets.
04:36However, because we want a custom one, we actually need to set it to none.
04:43So let's go over to the browser and take a look.
04:46I'll refresh the page, and there is our revised browser.
04:49It's pretty, but if you move the control, you see that pretty is all it is.
04:57However, we've got all of our basic elements in place to bring in the very first
05:01piece of functionality.
Collapse this transcript
Modifying display interactively
00:00As noted earlier, the basic range input type is lacking any way of showing the
00:05current value of that range control.
00:07In the previous lesson, we added a text field to hold the value.
00:11In this lesson, we will bring in some basic JavaScript to link the range
00:15element to the text field.
00:17Let's head over to the code editor, and we want to scroll all the way down to
00:21below the footer, where I'm going to put in a basic script tag, so I'll make
00:26an extra line after footer and close off the tag, and Aptana gives me my closing tag.
00:31Let me just open that up a little bit and scroll down so you can see what's going on.
00:37Now we're going to define the function that I've called Print Value, that will
00:42set up a couple of variables to get the ID of the slider, as well as the one
00:48from the text field. And once we have that, we'll be able to set one value to the other.
00:54So we start with a function and as I said, I'm going to call this printValue.
01:01And the first argument is sliderID, textbox.
01:06Now, let's open up a curly brace, and we'll set the first of our two variables, x,
01:12so I'm going to set x =, and here's a little JavaScript function,
01:19document.getElementById. And the first element we want to get is the text box.
01:29Close that with a semicolon, and then we set up our second variable, which I'll
01:33use y. And again, we use document. getElementById. And of course this time it's the slider ID.
01:46Next, let's set the x value--that's the value of the textbox--equal to the value
01:53of y, which is the slider.
01:55So we enter x.value = y.value.
02:00Now, before we test this, I want to add in one other JavaScript line within this
02:05script tag, and that's a line of code that will initialize the text field.
02:11So for that, we'll use the window.onload command and set that equal to a
02:17function call, and the function we're going to call is the one we just wrote,
02:22printValue, and the arguments that we'll pass it are the two IDs that we previously set.
02:30The first is slider and then the second is range value.
02:37Finally, let's insert an onchange event to the range control itself.
02:41So let's go back up to line 67 and I'm going to add an attribute right
02:47towards the end here,
02:49of onchange = and then we'll put in our function name which is printValue, and
02:57pass it in the two arguments.
02:58The first argument is the ID slider and the second argument is the ID for the
03:06textbox, which is range value.
03:08Let's save our page and then head on over to the browser.
03:13I'll click Refresh and there is the initial value of 400, and now when I drag it,
03:20I get the lower values in increments of 25.
03:24Looking good. Of course in and of itself, it's not where we want it to be, but
03:29now we're ready to bring in the necessary JavaScript functionality.
Collapse this transcript
Establishing JavaScript functions
00:00Time to hook up our range input type to the other elements on the page.
00:04First, we'll use a little jQuery magic to add data prices to our paintings, and
00:10then we'll insert the necessary JavaScript to filter the paintings by price.
00:14So in my code editor, I'm going to scroll down to the bottom and I'm going to
00:19add another script tag here.
00:21You could combine these if you want, but I'm going to do this just to keep
00:25everything a little distinct so it makes it easy to track. And again make a little bit room.
00:30Now this time, instead of putting in the code by hand, I actually have a code
00:35snippets file that's also found in that same folder, called code snippets.js,
00:41that I'm going to go to as a source. And we're going to bring in first this
00:46initial code block that you see up op.
00:48Now, this is just a simple jQuery declaration that uses a jQuery function to
00:55attach data to an element at the DOM level.
00:58As you can see, what we're doing here is, for each of the paintings, which have an
01:03ID of painting one through six,
01:05we're adding in a sequential ID-- just to make it easy to work with--and
01:10the individual prices.
01:11So I'm just going to copy this code block and go to index.htm inside my script
01:18tag and paste it in.
01:19Now that will set up the data prices for all of the paintings. Next, we're
01:24going to read them in.
01:26Let's go back to my code snippets file, and let's start by walking through
01:31the code initially.
01:33So after the document is ready, I declare value on line 12 called the value, and
01:39then we set up a simple function that monitors the change event of the slider.
01:46And anytime there is a change it gets whatever the value is that's in the Range
01:52Value text field and assigns it to our variable, the value, and then it calls
01:58another function called Filter Items and passes the value variable.
02:04The next code block, starting on line 20, spells out what actually happens in the
02:09function filter items.
02:11So it takes one argument, which I've called Price Criteria, and that's the same
02:16value as the variable the value. And once it has the price criteria, it
02:21loops through each of the paintings that are in the gallery and performs a
02:26series of functions.
02:27The key one, in line 24, is where it sets the data that's in a particular item to
02:35another variable called item data. And now that we have that, we can examine
02:40item data to see if the price that's in item data is less than or equal to the
02:46price criteria, which you'll recall is the range value.
02:50If it does, then we make sure that the opacity is set to 1 and we also set
02:54another property called matching to True.
02:57If the price criteria is not less than or equal to the item data price, then
03:03we reduce the opacity to .5, dimming it down, and set the itemData.matching to false.
03:10Now the itemData.matching I didn't have to include, but I wanted to show you how
03:15you could also keep track of things like this by adding in another property.
03:20All right, let's copy and paste our code, make sure it's inside the same script
03:25tag, paste that in, save it, and now let's head on over to the browser, refresh
03:33the page, and I'll scroll down just a little bit here so we can see how the
03:38paintings are reacting when I drag the slider. And there you can see that it is
03:43in fact starting to reduce the price.
03:45So when I'm at 200, both eye which is 400; arrow, which is 250; and also rabbit, which is
03:53350; are more than the value I've specified in the Price slider, and so they are dimmed.
04:01If I bring it back up to 325, I can now afford arrows at 250, but I still can't
04:10afford rabbit or eye.
04:12Awesome! Our users can now filter the items in the store by price, just by moving the slide.
04:20This technique could be applied to any sort of range-based filtering, such
04:25as the number of stars received, number of rooms in the house, whatever you
04:29can think of.
Collapse this transcript
3. Dragging Items to Compare
Establishing drag-and-drop functionality
00:00For such a high-level effect like a drag-and-drop--what?
00:04You mean I can actually drag pieces of a web page a round and they do something?
00:08For such an effect, drag-and-drop is really pretty easy to implement.
00:12And the first part on the HTML side is what my dad lovingly called dirt simple.
00:18So, let's go on over to our code editor.
00:19And I'm going to scroll down to where we have the elements that we want to drag,
00:24the paintings that are in this article tag with an id of gallery.
00:29And there are six paintings in all,
00:30each of them within a div, with an id like painting1, painting2, and so forth.
00:37So, to make these draggable, all we need to do is to go into that div,
00:43add a new attribute--draggable--
00:46and set that equal to true. That's it.
00:50So, I'm going to copy draggable=true and then in each of the divs, paste it in.
00:55Believe it or not, that's it.
01:01There is nothing to demo at this point--
01:03we will need to bring in some JavaScript first--
01:05but our drag-and-drop functionality is really good to go as far as the HTML
01:11is concerned.
Collapse this transcript
Adding JavaScript events
00:00In the previous lesson, we struggled arduously with the HTML for drag and drop.
00:06Okay, okay, there was only one attribute per div.
00:09But now we're ready for the main event, and that's bringing in the JavaScript
00:13events that will interact with our user's actions.
00:17The first thing I'm going to do is bring in some help.
00:20When drag and drop was first introduced, browser implementation was all over the map.
00:25One developer, Guillaume Bort, took it upon himself to level the playing field
00:30with his native drag-and-drop routines, which you can find on GitHub.
00:36I often include his library because it really helps browser compatibility.
00:40So, lets head all over to our source code, and I'm going to drop in this script include.
00:47And this is located in the _scripts, and it's called jquery.ndd, for
00:57native drag and drop, .js.
01:01Let's set the type=text/javascript and then close off this tag.
01:08Now, I'm going to link in another script that I've already placed in _scripts
01:14folder for this lesson.
01:16So, let's add in another script tag, and we'll set the src=_scripts/dragdrop.js.
01:27Again, I'll set my type=text/javascript and close the tag>.
01:35Now, let's take a moment to look at the two files that we included.
01:40First, I'll take a real cursory look at jquery.ndd.js.
01:45As you look at the various functions here, you can see that basically what
01:50he's doing is making it easy for the drag and drop to work across a number of
01:56different browsers.
01:57The first issue that's addressed, starting on line 21, for WebKit browsers like
02:02Safari or Google Chr1ome.
02:05Next, he fixes the events so they work across the board, and so on.
02:09You can go through all of the various functions that you see here
02:13and really get a behind-the-scenes look of how everything works.
02:17On the other hand, you can just use the library.
02:21Now, let's spend a little bit more time working with the custom code that's in dragdrop.js.
02:28Almost all the code that's located in this file is used for binding a specific
02:33drag-and-drop event to one or more page elements.
02:37It starts with the elements that we're dragging.
02:40The first code block identifies the painting class, which are all of our
02:44paintings, as the element that we are going to bind the dragstart event to.
02:49Now, you'll notice also that as part of that dragstart, on line 3, data
02:54transfer is a set up.
02:56And what it's transferring is the ID of the element.
02:59Now, I also have tossed in some effects here so that the page will see more
03:03lively when you start to drag something.
03:05The headings will fade in fast.
03:06And there is a Hover state.
03:08And on hover, the images will fade out, and then when you let go of them, fade in.
03:15Next, we address the target area, which is in a div called favorites.
03:21Now, if we take a look at the HTML page--and I'll scroll down to the side
03:27section, past the paintings--
03:28we'll see the div on line 74. And that's where we have the heading of Favorites,
03:36and the big Drag Favorites section.
03:38Now, you'll also see in unordered list with the ID of faves.
03:42That's where the favorites will be listed.
03:45Let's had back to dragdrop.js. So, there are a number of events that are bound
03:51to the favorites ID.
03:54First, there's the dragover event.
03:56When you drag the element over the target, favorites, the background
04:00color changes to a gray.
04:02Now, you'll also see, in three out of these four bindings, the
04:07code evt.preventDefault.
04:10This is a necessary call that needs to be made for drag and drop to work properly.
04:15Now, if you leave the background color as shown starting on line 16, the
04:20background color goes back to its original--
04:22that purple color that you saw.
04:24Now, although there is no functionality added to the dragenter event, you
04:29still need to have the preventDefault function applied for everything to work smoothly.
04:34Finally, there is the bind drop function.
04:39This is where we transfer the data that we previously got, the ID,
04:44and bring that in to what's going to become our list.
04:47The rest of the code, from line 25 on down to 31, basically builds up the list item.
04:54And when it's done, it adds in a new LI tag with the ID and whatever's inside of that.
05:01After the item has been added to the list, we change the background color
05:05back to its purple and issue a final command, stop propagation, in order to
05:11close everything out.
05:12Okay, we've linked this code to our page.
05:15Let's go to index.htm, save those changes,
05:19and now return to the browser. Refresh the page.
05:22So, let me scroll down first, so we can see all of our paintings and our
05:26favorites drop zone there.
05:28Let's say that I'm interested in patterns.
05:30So, I'll go over, click it up, drag it into the target area.
05:35We saw the background color changed there.
05:38You see the plus sign indicated on the icon itself. That's being handled by
05:43the operating system.
05:44I'll drop it, and there is patterns $200, the same price as listed on the main
05:51gallery section. Looking good.
05:53So, there's one favorite.
05:55Now, let's pick up arrows and bring that in.
05:58Again, the background color changes, and when I drop it, the background color
06:03reverts, and I have arrows at 250 listed here.
06:06So, our drag and rop is really a beauty to behold,
06:09but right now its actions are only temporary.
06:12Watch what happens if I refresh the page. Poof!
06:16All of our favorites are gone, which kind of defeats the idea of favorites.
06:20So, in the next chapter, we'll integrate another HTML5 technology to make it
06:25all better.
Collapse this transcript
4. Storing Wish List Items Locally
Saving list items
00:00Local storage, or web storage, is another advanced technology under the HMTL5
00:05umbrella that does a great job simply.
00:08Let me show you how easy it is to store items in the browser's local storage.
00:14So, we have our index.htm file opened, but we don't want to put the necessary code here.
00:20We're going to instead move it to dragdrop.js, since it is essentially dealing
00:26with the dropped items that we're going to save.
00:29So, the first thing I need to do is to declare a variable,
00:32and I'm going to put that right at the top of the page.
00:34So, I'll enter var, and I like to name my variables starting off with the
00:39article V. So, theFaves. And we're going to set that equal to document.getElemantById('faves');.
00:51Make sure to put that semicolon at the end.
00:54Now, what is the element ID faves?
00:56Let's go back to the HTML for just a sec.
01:00I'll scroll down to where my faves are.
01:04And here you see it's the ID in the unordered list. All right,
01:09let's had back to dragdrop.js, and now I'm going to move down the page.
01:15And this is part of the drop event.
01:18So, after the item has been dropped, we want to call a custom function,
01:22and I'm going to put this call to the custom function above the
01:25evt.stopPropagation.
01:27So, let's make a little error here so we can see it clearly.
01:31And I'm just going to call this custom function saveFaves. All right!
01:37Now, we'll enter our new custom function below the binding.
01:41And so I'll enter a function, give it its custom name, put in a curly
01:48brace pair, and then just the single line in order for us to save the item to local storage.
01:55So, we call localStorage.setItem and that's a capital I. And then in parentheses
02:05we have two arguments.
02:07The first is the item name that you're going to be saving.
02:09And I'm going to be calling it favorites, and then follow the argument with a comma.
02:15And now what is it that we are going to be saving into favorites?
02:19Well, we're going to go back our variable that we set up at the top of the page,
02:24theFaves, and we're going to pull in its inner HTML.
02:29So, if you recall, theFaves was the unordered list and its inner HTML are all the list items.
02:36So, type in theFaves.innerHTML, and make sure to put a semicolon at the end.
02:45Now, with this code, we have grabbed and stored whatever is in the
02:48unordered list faves,
02:50and we can do this without worrying, because local storage has a much bigger
02:54limit than cookies do: 5 megabytes.
02:56So, let's save dragdrop.js, and now we're ready for final step:
03:02displaying the items from local storage.
03:05And it's just as straightforward at storing them.
Collapse this transcript
Retrieving list items
00:00It's time for the final step of our project.
00:03In the previous lesson, I showed you how ridiculously easy it was to save
00:07items in local storage.
00:08In this lesson, we'll cover how to retrieve and display them. Ao I have my
00:13dragdrop.js file where we put in the saveFaves routine last, and now let's bring
00:19in the loadFaves routine.
00:21So I'm going to put it at the tail end of the JavaScript code block here, not in
00:28any function, so it will run automatically when the page loads.
00:33So there is a call to loadFaves, and now let's put in that code itself.
00:39I'll make a little extra room there, so we can see what's happening. So it's
00:44function loadFaves curly brace pair.
00:49Now the first thing we want to make sure is that the function that we're trying
00:53to load exists. So we'll use an if statement, if (localStorage.get
01:02item) and then another curly brace and then another set of parentheses and quotes,
01:08and we want to check to see if the items favorites is the one that exists.
01:14So if that exists--and let's put in another curly brace pair here--then what we
01:19want to do is get it and then assign what we get to the inner HTML of the
01:27unordered list, which we know as the variable the faves.
01:31So, it's theFaves.innerHTML = localStorage.getItem, and in parentheses,
01:42favorites, semicolon at the end and save, and that's it.
01:48Let's check our work in the browser.
01:50I'll refresh the page. Now, let's scroll down, and let's say this time we want the eye,
01:59the $400, one of our favorites.
02:01It shows up in our list. And let's bring in rabbit as well.
02:05Okay, so I'm going to just copy this URL, and open up a new tab, paste it in, and
02:15hit Refresh, and then when I scroll down, there are my favorites. Well, congrats!
02:22Your ecommerce catalog is now greatly enhanced and totally engaging, with three
02:28interwoven HTML5-related technologies.
02:31I'm sure your users will appreciate the upgrade to their experience.
Collapse this transcript
Conclusion
Next steps
00:00Thanks for watching HTML5 + UX: Engaging Ecommerce.
00:05If you would like to dig deeper into drag and drop, check out Bill
00:09Weinman's course in the online training library at lynda.com called Drag and Drop in Depth.
00:15Thanks again and have a great day.
Collapse this transcript


Suggested courses to watch next:

CSS: Core Concepts (8h 49m)
James Williamson

HTML5 First Look (4h 28m)
James Williamson


CSS3 First Look (6h 34m)
James Williamson


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked