0

I found this very nice script in jquery to edit the contents of a table on double click. Now i am trying to add more functionality to the table by adding button. The first function i am trying to add is "add". Take a look at my fiddle and things will be clear

Everything seems to works ok at this moment. However when I add a row on click of add, it does not allow me to edit the contents like the other rows

HTML

<table class="editableTable">
    <thead>
        <tr>
            <th>Code</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>001</td>
            <td>Pedro Fernandes</td>
            <td>[email protected]</td>
            <td>(21) 9999-8888</td>
        </tr>     

    </tbody>
</table>
<td style="text-align:center;">
    <button onclick="addrecord()" >Add</button></td>

JQUERY

$(function () {
    $("td").dblclick(function () {
        var OriginalContent = $(this).text();

        $(this).addClass("cellEditing");
        $(this).html("<input type='text' value='" + OriginalContent + "' />");
        $(this).children().first().focus();

        $(this).children().first().keypress(function (e) {
            if (e.which == 13) {
                var newContent = $(this).val();
                $(this).parent().text(newContent);
                $(this).parent().removeClass("cellEditing");
            }
        });

        $(this).children().first().blur(function () {
            $(this).parent().text(OriginalContent);
            $(this).parent().removeClass("cellEditing");
        });
    });
});
function addrecord(){
      $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
}

5 Answers 5

4

Change

$("td").dblclick(function () {

to

$(".editableTable").on("dblclick", "td", function () {

The difference between the two is that the former adds the event to the existing TDs but will not add the same event on any TD added dynamically, which you are trying to achieve. The latter however takes care of any TD added dynamically as well.

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

Comments

0

The code to make it double-clickable only runs at the start, so that code will not apply to any new rows you create. A really dirty hack is to just copy the code into your function, although there is bound to be other ways.

    function addrecord(){
      $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
      $("td").dblclick(function () {
        var OriginalContent = $(this).text();

        $(this).addClass("cellEditing");
        $(this).html("<input type='text' value='" + OriginalContent + "' />");
        $(this).children().first().focus();

        $(this).children().first().keypress(function (e) {
            if (e.which == 13) {
                var newContent = $(this).val();
                $(this).parent().text(newContent);
                $(this).parent().removeClass("cellEditing");
            }
        });

        $(this).children().first().blur(function () {
            $(this).parent().text(OriginalContent);
            $(this).parent().removeClass("cellEditing");
        });
    });
}

Comments

0

OK you have static table, you register event listener on each td, and then you add new row. Of course there's no click listener in that point. You need to register listener after creating new row:

var row = $('<tr><td>004</td><td></td><td></td><td></td></tr>');
row.find("td").dblclick(mylistener)
row.appendTo('table');

Comments

0

use on() , as live() is depriciated

Comments

-1

.live will do the trick..

$(function () {
$("td").live("dblclick",function () {
    var OriginalContent = $(this).text();

    $(this).addClass("cellEditing");
    $(this).html("<input type='text' value='" + OriginalContent + "' />");
    $(this).children().first().focus();

    $(this).children().first().keypress(function (e) {
        if (e.which == 13) {
            var newContent = $(this).val();
            $(this).parent().text(newContent);
            $(this).parent().removeClass("cellEditing");
        }
    });

    $(this).children().first().blur(function () {
        $(this).parent().text(OriginalContent);
        $(this).parent().removeClass("cellEditing");
    });
});
});
function addrecord(){
  $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
}

3 Comments

use .on() with the newer versions of jQuery as .live() was removed from them
does it work like .live did?`I mead does it also match for elements whcih will be added later on?
Yes, look at mohkhans answer, if given the right parameters it will work for dynamically added elements. api.jquery.com/on

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.