Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Edit button jquery function

How do I make my to do list able edit input through the edit button using jquery? It is a to do list and i want users to be able to edit the input Thanks in advance

<header data-role="header">
  <h1>To Do List</h1>
  <a href="#">Home</a>
</header>

<div role="main" class="todo-     container">
  <div class="input">
    <input type="text" class="note" placeholder="Input new note..">
    <button id="addButton">Add</button>
  </div>

  <ul class="todo-list">
    <li>
      <div class="button-center">
      </div>
    </li>
  </ul>

  <script>
    $(document).ready(function() {
      $('#addButton').click(function() {
        var task = $(".note").val();
        $(".todo-list").append(`<li>     <span>Task: ${task}</span><div class="button-center"><button class="deleteButton">Delete</button><button class="editButtton">Edit</button>   </div></li>`);
        $(".note").val("");
      });
      $(document).on('click', '.deleteButton', function() {
        $(this).parent().parent().remove();
      });
    });
    $('#editButton').click(function() {});
  </script>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I have done something like this : example

user will be able to edit by double click at the text and an input field will showed up. user can click the edit button once he/she finished edit and the text will be updated.

JS

$('#addButton').click(function() {
 var task = $(".note").val();
 $(".todo-list").append(`<li>Task: <span class='task'>${task}</span><div 
 class="button-center"><button class="deleteButton">Delete</button><button 
 class="editButton">Edit</button>   </div></li>`);
 $(".note").val("");
});

$(document).on('click', '.deleteButton', function() {
 $(this).parent().parent().remove();
});

$(document).on('dblclick', '.task', function() {
 var task = $(this).text();
 $(this).text('');
 $(this).append(`<input type="text" value="${task}" />`);
});

$(document).on('click', '.editButton', function() {
 var task = $(this).parents().siblings('span').children('input').val();
 $(this).parents().siblings('span').remove('input');
 $(this).parents().siblings('span').text(task);
});
about 4 years ago · Juan Pablo Isaza Report

0

I've modify your code and added the edit functionality. It's supposed to work as you will expect so I will not say much. Kindly try this

$(document).ready(function() {
    var $note   = $('.note');
    var $taskId = $('.task-id');

    $('#addButton').click(function() {
        var task = $note.val().trim();
        var id   = $taskId.val();

        if( task.trim() === '' ){
            // do nothing
        }else if( id !== '' ){ // edit
            $(".todo-list").find('[data-id="'+id+'"] .task').text(task);
        }else{ // add todo
            id = new Date().getTime(); // you can make it more unique
            $(".todo-list").append(`
                <li data-id="${id}">
                    <span class="task">${task}</span>
                    <div class="button-center">
                        <button class="deleteButton">Delete</button>
                        <button class="editButton">Edit</button>
                    </div>
                </li>
            `);
        }

        $note.val('').focus();
        $taskId.val('');
        $(this).text('Add');
    });
    
    // delete and edit action
    $(document).on('click', '.deleteButton', function() {
        $(this).closest('li').remove();
    }).on('click', '.editButton', function(){
        let $li = $(this).closest('li');

        $note.val( $li.find('.task').text() );
        $taskId.val( $li.data('id') );
        $('#addButton').text('Update');
    });

    // cancel update if .note text is cleared
    $('.note').on('input', function(){
        if( $(this).val() === '' ){
            $taskId.val('').focus();
            $('#addButton').text('Add');
        }
    })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input">
    <input type="text" class="note" placeholder="Input new note..">
    <input type="hidden" class="task-id">
    <button id="addButton">Add</button>
</div>
<ul class="todo-list"></ul>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!