Woody1193
Woody1193

Reputation: 7960

jquery function not calling on click

I have a function like so:

last_value = null;
$('td').click(function(e) {
  console.log($(e.target).attr("id"));
  if ($(e.target).is("td")) {
    var the_form = $("#edit-form").detach();
    if (last_value) {
      $("#" + last_value.attr("id")).replaceWith(last_value);
    }

    last_value = $(e.target).clone();
    $(the_form).removeClass("hidden");
    var the_input = $(the_form).find("input.form-control");
    $(the_input).attr("name", $(e.target).attr("id"));
    $(the_input).attr("placeholder", $(e.target).text());
    $(e.target).css('padding-top', 1);
    $(e.target).css('padding-bottom', 1);
    $(e.target).text("");
    $(e.target).append(the_form);
  }
});

This is supposed to take a table cell, and produce an inline form populated with the cell contents, which it replaces. Additionally, code has been added so that when a different cell is clicked, the contents revert to their original values. However, the problem I'm running into is this: suppose I click one cell, A. The form appears the way it should. Then suppose I click cell B. The form then "moves" to that cell, and the contents in cell A revert to their original values. Now suppose I click on cell A again. In this case, not only does the form not appear, it stays in cell B. In fact, the console.log doesn't even fire. What am I doing wrong here?

Upvotes: 0

Views: 98

Answers (2)

Woody1193
Woody1193

Reputation: 7960

As was suggested in the comments under the OP, (thanks @Mohammad Akbari), changing the code to

$('table').on('click', 'td', function(e){
       console.log($(e.target).attr("id"));
       if ($(e.target).is("td")) {
            var the_form = $("#edit-form").detach();
            if (last_value) {
                $("#" + last_value.attr("id")).replaceWith(last_value);
            }

            last_value = $(e.target).clone();
            $(the_form).removeClass("hidden");
            var the_input = $(the_form).find("input.form-control");
            $(the_input).attr("name", $(e.target).attr("id"));
            $(the_input).attr("placeholder", $(e.target).text());
            $(e.target).css('padding-top', 1);
            $(e.target).css('padding-bottom', 1);
            $(e.target).text("");
            $(e.target).append(the_form);
      }

});

fixes the problem.

Upvotes: 0

Naim Malek
Naim Malek

Reputation: 1184

Try with this

$(document).find('td').on('click',function(e){

           e.preventDefault();
           console.log($(e.target).attr("id"));
           if ($(e.target).is("td")) {
                var the_form = $("#edit-form").detach();
                if (last_value) {
                    $("#" + last_value.attr("id")).replaceWith(last_value);
                }

                last_value = $(e.target).clone();
                $(the_form).removeClass("hidden");
                var the_input = $(the_form).find("input.form-control");
                $(the_input).attr("name", $(e.target).attr("id"));
                $(the_input).attr("placeholder", $(e.target).text());
                $(e.target).css('padding-top', 1);
                $(e.target).css('padding-bottom', 1);
                $(e.target).text("");
                $(e.target).append(the_form);
          }

    });

Hope this will help you.

Upvotes: 1

Related Questions