Viewers: in countries Watching now:
This lesson is a bit of a mixed bag. We need to add three small functions, to handle three different tasks. First, we want to make sure that the main input field works properly if the user should press Enter or Return. Then, we want to update the value of a list item, that's being edited. And finally, we want to lay the groundwork for reacting when an item is dragged to a new location. So, I thought, who doesn't love a little potpourri every now and then. Let's get started. Here's the index.htm file from the chapter three 03_03 folder.
And we'll start off with the jQuery $, and then our selector. Make sure we put it in parenthesis and single quotes. And this is going to be an input tag with the type equal to, double quotes text, and then after the closing parenthesis, dot and on. So, what we're doing now is setting up the selector for the right kind of input tag and then look for the Key Down event. We're using the .on syntax from jQuery. That asks for first the event, and we'll use the key down event, in single quotes, followed by comma, and function, followed by parenthesis, with a E for error handling inside of that, and then a curly brace to hold our code.
Now we'll eventually need to close off the parenthesis that we put in right after on, so I'll go ahead and add that in, as well as a closing semicolon. I can forget that from time to time so, for me personally it's always good to go ahead and put that down. Next, we'll set up a variable to get the actual character code. We're going to need to use a slightly complex conditional statement to cover all of our bases. So follow along carefully. Set the variable to key, and we'll set that equal to e.charCode, short for character code, of course.
Followed by question mark, and again e.charCode colon, and then basically the same thing again, but with keyCode, keyCode question mark, e.keyCode colon 0. Close all that off with a semi-colon. Now once we get our key, we check to see if the key is equal to 13. 13 is the value for the Enter or the Return key. And if we have that, we'll do a couple things. First, we'll stop it from its normal functioning. Then, we'll put the focus on the next input field, which happens to be our submit button.
And to do that, we'll put in a couple lines of code. First, we'll create a variable called inputs, and set that equal to whatever field we're on, using the jQuery vis operator, followed by a dot closest, another jQuery conceit. Is in a form in parenthesis dot find open parenthesis single quotes colon input colon visible, close off your parenthesis and the line with a semi-colon, let me scroll us down a little bit here so you can see that easier. And next we're going to set the focus, as I said.
We'll put in our variable name inputs, where it's equal to inputs.index, this plus one, that moves it out one, .focus. All right. That function's complete. Now, we want to handle any edits that we make to our list items. We're going to look for a change event for the class .listItem, which is inside the list. So let's set up our event handler, and I'll start off with pointing to the list as a selector. This is the UL tag that holds all of our list items. And we'll use .on, open a parentheses, the event we're looking for is change, and then we're going to target a specific class within the list, and that's the class of the list item itself, which, not coincidentally is called list item, so .listItem.
And then after that single quote, another comma and we open up our function, again with E as the arrow handler, and let's put our closing curly brace, and after our closing parenthesis, and semi-colon. So now within that function, once this event is triggered, we'll pick up the just entered current value. So this is going to be a variable called currentValue, and we'll set that equal to this.val. This is the value for the input field, that's in the list item, that the user is changing.
Once we have that, we can swap out the input field's attribute. So again, we'll use the jQuery this, and we want to an change attribute, so we use .attr, open parentheses, what attribute do we want to change? We want to change the one called value, and what do we want to change it to? Our variable currentValue. Now we need to do it this way, rather than just use the jQuery val property, to ensure that the dom is changed. Which is critical for saving our list later. Finally, let's make a little bit more room.
We're going to create a, for now, empty little function, that checks to see if our list has been re-sorted. We"ll need this when it comes time to automatically save our list. This functionality is, frankly, a bit of a bear with the standard drag and drop API. But the HTML 5 sortable plugin makes it really straightforward. So it's basically a binding, we'll pin point our class assortable, and then after the parentheses, we're calling this sortable function, and we'll bind that using the sort update event.
And we'll set up a function, again followed by a curly brace, and then close off that curly brace with a closing parentheses and semi-colon. Okay. That's all we need for now. I'll save the page, and in the next lesson, we'll put in the code for removing list items, both individually, and collectively.
There are currently no FAQs about HTML5 Projects: Advanced To-Do List.
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.