4

I'm trying to create a To Do list, and when the user enters a new task, and clicks the button, the javascript should create a li element containing a span that holds the user's entry, then add that li element to the ol in my HTML. My HTML looks like this:

<body>
    <h1>To Do:</h1>
    <section>

        <input type="text" id="add_todo">
        <span id="add_task_error">&nbsp;</span>
        <input type="button" id="add_task" value="Add task">

        <div id="empty_message" class="open">
            <h3>You have no tasks left to accomplish!</h3>
        </div>

        <div id="tasklist">
            <ol class="list">

            </ol>
        </div>

    </section>
</body>

This is the function that is not working:

        var newSpan = $('<span>input</span>').addClass("task");
        //wrap it in a <li> element
        newSpan = (".task").wrap("<li></li>");
        $(".list").append(newSpan);

I also tried it this way:

         var new_task = $('<li>*</li>').addClass('task');
         new_task.appendTo('ol.list');
         new_task.setAttribute('id', 'new_task');
         $("#new_task").text(input);

Both ways did not work- when I clicked the Add Task button (which is not the problem- I tested it), nothing happened on the screen...

What am I doing wrong???

1
  • can you share a fiddle jsfiddle.net ? Commented Dec 9, 2015 at 8:07

5 Answers 5

4

Try this

$(document).ready(function(){
  $('#add_task').click(function(){
    var task = $('#add_todo').val();
    var html = '<li><span>'+task+'</span></li>';
    $('.list').append(html);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>To Do:</h1>
    <section>

        <input type="text" id="add_todo">
        <span id="add_task_error">&nbsp;</span>
        <input type="button" id="add_task" value="Add task">

        <div id="empty_message" class="open">
            <h3>You have no tasks left to accomplish!</h3>
        </div>

        <div id="tasklist">
            <ol class="list">

            </ol>
        </div>

    </section>

Sign up to request clarification or add additional context in comments.

Comments

0

Create the element, set all the attributes and when, you are done, add it to the ol.

var new_task = $('<li></li>').addClass('task');
new_task.text($("#add_todo").val()); //this is the value of the input
new_task.attr('id', 'new_task'); //use attr instead of setAttribute    
new_task.appendTo('ol.list');

FIDDLE

3 Comments

Yay! It works! Your solution seems so simple- it's pretty much just changing the order of how I did it originally, but I was stuck on this for days...
@Meg not just this, the use of attr is important.
How is it different than setAttribute?
0

Hope this works for you

JS code:

$("#add_task").click(function(){
    var value = $("#add_todo").val();
    $(".list").append("<li class='task'><span>"+ value +"</span></li>")
});

Here is the working Plnkr

Comments

0

This should be your code. Call addLI() on click of your button

<input type="button" id="add_task" value="Add task" onclick="addLI()">

function addLI() {
    //check for empty value
    if ($('#add_todo').val() == "") {
        alert("Please Add Todo.");
        return;
    }
    //generate html for li
    var html = "<li class='task' id='new_task'><span>" + $('#add_todo').val() + "</li>";

    //append li to order list
    $(".list").append(html);
}

Comments

0

Also try to hide the div on which you are showing the message before adding any new task.

<div id="empty_message" class="open">
            <h3>You have no tasks left to accomplish!</h3>
        </div>

$('.open').hide(); on click event of add task

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.