How to edit the td value when double click on it

5.6k views Asked by At

I want to edit the td value of particular id when i double click on it. I wrote the logic. In below code 'get()' function will return 10 status which is assigned to a table td. If i double click on the any status I need feature of in-place editing and saving. But I don't know why it is not working. Please anyone help me.

    <html>
    <head><title></title></head>
    <body>
    <div id="body" >
    </div>
    </body>
    </html>
    <script>


                    $(document).ready(function(){

                    var table='<table>';
                        table += '<tr><th style=""> Status</th></tr>';
                        table += '</table></br>';

                    $("#body").append(table);
                    var $tbody = $('<tbody>').appendTo('#body table:last');
                    $.ajax({
                    type : 'POST',
                    url : '@routes.Application.get()',
                    data : {
                    itemupc : item[0]
                    },
                    beforeSend:function()
                    {   

                    }, 
                    success : function(items) {

                    $.each(items, function(j, itemsdetails) {



               if(itemsdetails[3]=="R")

                    $tbody.append('<tr><td  id="my'+itemsdetails[0]+'" class="editableTD">0</td></tr>');

                                        }); 
}    
    });
               $("#item_content").on('dblclick','.editableTD',function(e){ //assign event to editableTD class
                    e.stopPropagation();
                    var currentID=$(this).attr("id"); //grab the current id instead
                    var currentValue= $(this).html();
                    inlineEditSave(currentID,currentValue);
                });
                function inlineEditSave(currentElement,currentValue)
                    {
                    //$(currentElement).html('<i class="fa-li fa fa-spinner fa-spin"></i>');
                        $(currentElement).html('<input type="text" class="thVal" value="' + currentValue + '" />');
                        $(".thVal").focus();
                        $(".thVal").keyup(function (event) {
                            if (event.keyCode == 13) {
                                $(currentElement).html($(".thVal").val().trim());

                            }
                        });
             $(document).click(function () {

                    $(currentElement).html($(".thVal").val().trim());

            });
    }
                    });

            </script>
1

There are 1 answers

3
Ron E On BEST ANSWER

Given that you are using Font Awesome can I also assume you're using bootstrap? If so, is there any particular reason why you are coding the logic yourself? There is a library for this called x-edtiable that handles all of the heavy lifting for you:

http://vitalets.github.io/x-editable/

Demos / Usage examples can be found here:

http://vitalets.github.io/x-editable/demo-bs3.html