2

I have a table of records. when a user double-clicks on each <td> it converts to <input type="text"> and after click outside it, it must be saved. but the problem is when click inside of the specific <td> it does that.

//double click ->then convert to input
$(document).on('dblclick', 'tbody tr td', function() {
  var tmp = $(this).text();
  var name = $(this).attr("data-name");
  $(this).html('<input type="text" id="tmp_ed" value="' + tmp + '"/>');
  $(document).on('click', function() {
    if ($(this).attr('id') !== "tmp_ed") {
      var item = $('#tmp_ed').val();
      var tr = $('#tmp_ed').parent().parent();
      var id = tr.find('i').attr('id');
      var update = {
        id: id,
        request: "update",
        val: item,
        column: name
      };
      $.ajax({
        url: "update.php",
        method: "POST",
        data: update,
        success: function(e) {
          alert(e);
        }
      });
      $(this).parent().html(item);
      $(this).remove();
    }


  });
});

1
  • Use onChange on input Commented Aug 4, 2017 at 13:51

2 Answers 2

1

Here is a small working demo of your requirement without the AJAX call to update the input value in your database:

//double click ->then convert to input
var currentEle = '';
$(document).on('dblclick', 'tbody tr td', function(e) {
  e.stopPropagation();
  currentEle = $(this);
  var tmp = $(this).text();
  var name = $(this).attr("data-name");
  $(this).html('<input type="text" id="tmp_ed" value="' + tmp + '"/>');
  $("#tmp_ed").focus();
});

$(document).click(function(e) {
  if ($(e.target).closest('td').length == 0)
    $(currentEle).html($("#tmp_ed").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>test</td>
  </tr>
</table>

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

Comments

1

How about something like

window.onclick = function(event) { 
    if (event.target != $('#tmp_ed')){
        //Your saving stuff
    }
}

JSFiddle: https://jsfiddle.net/gss6ezko/

1 Comment

unfortunately the same issue.

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.