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

Removing list items

From: HTML5 Projects: Advanced To-Do List

Video: Removing list items

One of my greatest joys in life is marking items off my list. I've even been known to write out a list item even though I've already done it just so I can scratch it out. What can I tell you. In this lesson, we'll add the functionality to remove list items one at a time, as well as wire up our clear all link so we can get rid of them all quickly and easily. Now we have the index dot html file from the chapter 3 03 04 folder open in our code editor, to the bottom of the page we go where our javascript lies and we'll go all the way to the bottom and add in our next set of functions.

Removing list items

One of my greatest joys in life is marking items off my list. I've even been known to write out a list item even though I've already done it just so I can scratch it out. What can I tell you. In this lesson, we'll add the functionality to remove list items one at a time, as well as wire up our clear all link so we can get rid of them all quickly and easily. Now we have the index dot html file from the chapter 3 03 04 folder open in our code editor, to the bottom of the page we go where our javascript lies and we'll go all the way to the bottom and add in our next set of functions.

So I need three functions to remove list items properly, one to show the red x when a user mouses over the list item, another to hide that red x when he or she mouses out, and then a third to delete the list item when the x is clicked. All three use the J query on function to detect event triggering so we're going to put the first of these after our sortable update code block. Now, the red x is hidden by default by CSS so let's add code first to show it on mouse over.

I'll put in my JQuery dollar sign for the selector, a pair of parantheses and a pair of single quotes. And we start by targeting the list, that's the UL tag .on and then parantheses in single quotes mouse over. Follow that with a comma and then we put in the item within the list that we are actually targeting which is LI, follow that with a another comma and then open up a function with own set of parenthesis and then a curly braise and I'll make a little room and put in my semicolon.

So the first thing we need to do once this event is triggered, is to find the Anchor Tag. That's the A tag. Within this particular list item. So I'm going to set up a Variable, call it thisA, and set that equal to Dollar Sign, Parentheses, this. That's going to refer to the particular list item that was mouse overed, .find. And what are we looking for? It's the a tag in single quotes. Put a semicolon at the end of that. And now, once we have that, we can target the variable, this a. We'll change the display property, which I'll put in single quotes.

And follow that by what ever it is that we're going to change it to and we're going to change it to block ; at the end and this little code block's done. Now the next code block where I hide the x on mouse out is pretty similar so let's just copy and paste the one we just did and make some changes. So I'll select it starting on line 204 go all the way down I'll do that paste it in and then we'll change mouse over to mouse out and change display block to display none okay now we're ready to actually remove the list item.

And this will happen if the user clicks the X link. So again, we'll use the on event to trigger it, but we'll look for a click. So, start off by targeting the outer list. Follow that with .on, and then open and close parentheses. Our event is click, comma, what we're actually targeting has a class name of removeListItem. That's the anchor tag, and then we'll put in our function with an e for error handling, and our {}, and let's not forget our closing ; there.

Now because we are clicking on a link, we don't want it to go to that link, so we'll use the e.preventDefault to prevent the default behavior. And then we just need one line of code to remove the item. So we want to again use the jQuery this operator. Now the this in this case that it's referring to is the anchor tag, and we don't want to remove just the anchor tag. We want to remove what's surround the anchor tag, its parent, in other words. So I'll put in parent and ( ), and then function remove.

Close that off with a semicolon, and we're done! Okay, our individual list items are handled. Now, let's bring our Clear All link online. Again, we'll use the ON function and prevent the normal default action of clicking a link. So, let's start there, but this time we're going to be targeting the ID doClearAll, This is the ID that's assigned to a paragraph tag that's holding the Clear All link. And we'll do .on ( ). The event is click. What we're targeting is the link that has an ID of clearAll, and put in a function for it.

We'll use our e for error handling. { }. Close it off with a ;. And as I said we want to prevent that normal default action from occurring so E dot prevent default, now comes the code to remove all the children of the list which is all the list items and that's easy its just one line of code but we'll target the list. More specifically the children of the list and what do we want to do, remove it, not to difficult right. Finally time for a little more cleanup first because we've gotten rid of all the list items we're going to reset the new list flag to true.

Then we're going to clear out the input field just in case there's anything left in there. The ID for that element is toDoItem, and we'll just set its value, its val, to nothing. Now because we have no list items, we don't want our clearAll showing, so we're going to hide the clearAll link. You'll recall that the id for the paragraph holding that is the one that's at the top of this code block and that's do clear all. And we'll set the css property of display to none. Now finally we're going to reset the focus to our input field.

And you can do that after identifying it by just putting in .focus(). I'll save my page, and now we're actually ready to tackle the final task, saving and restoring our list automatically.

Show transcript

This video is part of

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

13 video lessons · 10504 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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.