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

  • Home
  • SEARCH
  • 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 534843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:39:17+00:00 2026-05-13T09:39:17+00:00

I am using jquery to add mulitple new addTask form elements to a ul

  • 0

I am using jquery to add mulitple new “addTask” form elements to a “ul” on the page every time a link is clicked.

  $('span a').click(function(e){
    e.preventDefault();

     $('<li>\
     <ul>\
     <li class="sTitle"><input type="text" class="taskName"></li>\
     <li><input type="button" value="saveTask" class="saveTask button"></li>\
     </ul>\
     </l1>')
     .appendTo('#toDoList');
    saveTask();
});

These new nested ul elements all have an button with the same class “saveTask”. I then have a function that allows you to save a task by clicking on an button with the class “saveTask”.

// Save New Task Item 
function saveTask() {
    $('.saveTask').click(function() {
        $this = $(this);
        var thisParent = $this.parent().parent()

        // Get the value
        var task = thisParent.find('input.taskName').val();

        // Ajax Call 
        var data = {
            sTitle: task,
            iTaskListID: 29
        };

        $.post('http://localhost:8501/toDoLists/index.cfm/Tasks/save', 
            data, function(data) {

            var newTask = '<a>' + task + '</a>'
            thisParent.find('li.sTitle').html(newTask);
        });
        return false;
    });
}

This essentially allows the user to enter some text into a form input, hit save, and then the task gets saved into the database using ajax, and displayed on the page using jQuery.

This works fine when there is only one element on the page with the class “saveTask”, but if I have more than 1 form element with the class “saveTask” it stops functioning correctly, as the variable “var task” shows as “undefined” rather than the actual value of the form input.

  • 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-05-13T09:39:17+00:00Added an answer on May 13, 2026 at 9:39 am

    Don’t rely on the .parent() method. Use .closest('form') instead. So the following line:

    var thisParent = $this.parent().parent()
    

    should look something like this instead:

    var thisParent = $this.closest('form');
    

    EDIT:
    Based on the updated information you provided, it looks like when you’re trying to register the click event handler it’s failing out for some reason. Try this javascript instead as it will make use of the live event so that all the newly added items on the page will automatically have the click event autowired to them.:

    $(function(){
        $('span a').click(function(e){
            e.preventDefault();
    
             $('<li>\
                 <ul>\
                 <li class="sTitle"><input type="text" class="taskName"></li>\
                 <li><input type="button" value="saveTask" class="saveTask button"></li>\
                 </ul>\
                 </l1>')
                 .appendTo('#toDoList');
    
        });
    
        $('.saveTask').live('click', function() {
            $this = $(this);
            var thisParent = $this.closest('ul');
    
            // Get the value
            var task = thisParent.find('input.taskName').val();
    
            // Ajax Call 
            var data = {
                sTitle: task,
                iTaskListID: 29
            };
    
            $.post('http://localhost:8501/toDoLists/index.cfm/Tasks/save', 
                data, function(data) {
    
                var newTask = '<a>' + task + '</a>'
                thisParent.find('li.sTitle').html(newTask);
            });
            return false;
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Discovery on the fly sounds a lot like an extensibility/Add-In… May 13, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer I had the same problem, but i'm using c# on… May 13, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer maybe I don't get your question, but what about this:… May 13, 2026 at 11:48 pm

Related Questions

I am using jquery to append or remove a <ListItem> (rendered as <option>) from
I am using multiple asynchronous air.URLLoader objects, and would like the events fired to
Walter Rumsby provided a great answer to Where to place JavaScript in an HTML
I am developing DNN modules and I am using jQuery in some modules, I
I have Commenting system in my app. For a single video entry anyone can

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.