Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Handling edits

From: HTML5 Projects: Advanced To-Do List

Video: Handling edits

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.

Handling edits

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.

Let's go back down to the bottom of the page to add in our JavaScript code. We already have a mechanism in place to handle the user clicking on the add to list button. That's all the code that we inserted in the last lesson. But what happens if someone types in a new list item, and naturally hits Enter or Return? Well, it actually blanks out our existing list, at this point. Not what we want to have happen at all. So let's add a little code block, that looks for an Enter or Return, and deals with it appropriately. We're going to put this within our document ready function, but after the add to list item function, so I'll open up a couple of lines there.

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.

Show transcript

This video is part of

Image for HTML5 Projects: Advanced To-Do List
HTML5 Projects: Advanced To-Do List

13 video lessons · 9844 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
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

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: Advanced To-Do List.

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
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.