How do I get my unordered list items counter to work in jquery?
Hello there!! I created a to do list with a delete button on the right side of each list item.
When you type an entry it goes into the ul tag listing everything.
How do I get the " counter " of my unordered list items to dynamically reflect both added and deleted items from my " to do list "?
I basically need a numeric value displayed in my id called " counter ".
I have been working on this for a few days and I just do not know what to do.
Maybe there is something I am over looking that is so simple.
Need help asap please.
- ==============================================================
- <!-- /-------------// HTML CODE STARTS HERE //-------------/ -->
- <title>Random Task List</title>
- <h1>todo list</h1>
- <label><input type="text" id="enter_task" placeholder="Add items to list"></label>
- <input type="submit" id="add" value="Add">
- <p>
- <ul id="todo_list">
- </ul>
- </p>
- <p>There are <span id="counter"></span> items in list.</p>
- <!-- /------------// JAVASCRIPT - JQUERY //---------------/ --><script>
- function enter_task () {
- var text = $('#enter_task').val();
- $('#todo_list').append('<li>'+ text + '<input type="submit" class="done delete" value="Delete">' +'</li>');
- };
- /*
- $('#todo_list').on('click', '.edit', function(){
- $(this).parent().attr('contenteditable','true');
- });
- */
- $('#todo_list').on('click', '.delete',function(){
- $(this).parent().remove();
- });
- $(function() {
- $('#add').on('click', enter_task);
- });
- // This BELOW is what I wanted to do but it is not working at all.
- /*
- var todo_list = document
- .getElementsByIdName('todo_list');
- $('#add').click(function(){
- //$('#todo_list').appendTo('counter');
- $('#counter').text('There are now ' + todo_list.length + ' listed elements.');
- });
-
- */