Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:20:15+00:00 2026-06-18T04:20:15+00:00

Basically i’m creating a little to do list app, at the minute I append

  • 0

Basically i’m creating a little to do list app, at the minute I append new list items to a container on button click, this creates the list effect.

What I want to do is add a class to the last element of the list, using last:child works for this but what I need it to do is update the last element when new elements are added, so lets say I have 5 items in a list so the 5th item has class last, when I add a new item I want to remove the class from the old last item and add it to the new last item (that make sense?)

Here’s the code I have so far..

<div id="list_container">
    <div id="to_do">
        <ul id="tasks" style="padding:0;">      
        </ul>
    </div>
</div>

var items = [];

// Add new item
$('#add_btn').click(function(event){
    event.preventDefault();

    var list_item = $('#new_item').val();

    // Validation
    if(list_item == ""){
        $('#new_item').addClass('empty');
    }else{ 
        $('#new_item').removeClass('empty').addClass('normal');
        items.push(list_item);
        var lastEl = items[items.length-1];
        $('ul#tasks').append('<li><input type="text" class="to_do_item" value="' + lastEl + '"/></input></li>');
    }

    // Clear input field after adding
    $('#new_item').not(':button, :submit, :reset, :hidden').val('');

});

Anyone know how to achieve this? I’m think it can’t be that difficult but I can’t seem to think straight right now..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:20:17+00:00Added an answer on June 18, 2026 at 4:20 am
    ...
    var lastEl = items[items.length-1];
    $('ul#tasks li').removeClass('last');
    $('ul#tasks').append('<li class="last"><input type="text" class="to_do_item" value="' + lastEl + '"/></input></li>');
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a list of delivery checkboxes one for deliver to this address
Basically I have this server app I built in vc#, and for some reason
Basically I want to check the extended permissions the user has granted my app.
Basically I've made a form in Cakephp 2.1 with a jQuery button that appends
Basically I am trying to make the in-app purchase work, but with no luck.
Basically I'm trying to reprint this method every time a dropbox option is selected.
Basically, I want to do the opposite of this: how do i hide anchor
Basically, I have written a Spotify app thats includes only a single page (index.html)
Basically, from the form that I made I send customer_name and with this code
basically ive followed this tutorial here and have everything up and running and working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.