본문 바로가기
javascript

html 수정(edit), 저장(save) 버튼 만들기

by 바나냥 2020. 5. 18.
$(document).on('click', 'a.edit-row', function (e) {
  e.preventDefault();
  var _self = $(this);
  var row = $(this).closest('tr');

  row.find('.on-editing').removeAttr('hidden');
  row.find('.on-default').attr('hidden', true);

  row.children('td').each(function (i) {
    var _this = $(this);
    var content = $(this).text();

    if (!_this.hasClass('actions')) {
      _this.html('<input type="text" class="form-control text-center" value="' + content + '"/>');
    }
  });                    
}).on('click', 'a.save-row', function (e) {
  var _self = $(this);
  var row = $(this).closest('tr');

  row.find('.on-default').removeAttr('hidden');
  row.find('.on-editing').attr('hidden', true);

  row.children('td').each(function (i) {
    var _this = $(this);

    if (!_this.hasClass('actions')) {
      _this.html(_this.find('input').val());
    }
  });                    
});

댓글