$(function(){
  if(!$('div#turn_off_event_handler').length){
    $("#new_time_entry").on("submit", function(e) {
      e.preventDefault();
      url = this.action
      data = {
        'authenticity_token': $("input[name='authenticity_token']").val(),
        'time_entry': {
          'matter_id': $("select#time_entry_matter_id option:selected").val(),
          'date(2i)': $("select#time_entry_date_2i option:selected").val(),
          'date(3i)': $("select#time_entry_date_3i option:selected").val(),
          'date(1i)': $("select#time_entry_date_1i option:selected").val(),
          'duration': $("input#time_entry_duration").val(),
          'rate': $("input#time_entry_rate").val(),
          'description': $("textarea#time_entry_description").val(),
          'lawyer_id': $("select#time_entry_lawyer_id option:selected").val(),
          'billable': $("input#time_entry_billable").val(),
          'paid': $("input#time_entry_paid").val()
          }
        };
      $.ajax({
        url: url,
        method: "POST",
        data: data,
        dataType: "JSON",
        success: function(response){
          $(".new_time_entry").trigger("reset");
          $("input[type='submit']").removeAttr('disabled');
          $("#errors").empty();
          var li_item = undefined
          li_item += "<tr>"
          li_item += "<td>" + response.date + "</td>"
          li_item += "<td>" + response.description + "</td>"
          li_item += "<td>" + response.duration + "</td>"
          li_item += "<td>" + response.lawyer.name + "</td>"
          li_item += "</tr>"
          $('#show_matter_time_entries tbody').append(li_item)
          $("input[type='submit']").removeAttr('disabled');
         },
         error: function(response){
           $("input[type='submit']").removeAttr('disabled');
           $("#errors").html(response.responseText);
         }
       });
    });
  }
  if (!!$('#show_matter_time_entries').length) {
    $("#show_matter_time_entries").ready(function(e){
      //e.preventDefault();
      const matter_id = $("#matter-name").attr("matter-id")
      $.ajax({
        url: '/matters/' + matter_id + '/time_entries',
        method: "GET",
        dataType: "JSON",
        success: function(response){
          const table_header = "<tr><th>Date</th><th>Description</th><th>Duration</th><th>Lawyer</th></tr>"
          $('#show_matter_time_entries').append(table_header)
          for (var i=0; i < response.length; i++){
            var li_item = undefined
            li_item += "<tr>"
            li_item += "<td>" + response[i].date + "</td>"
            li_item += "<td>" + response[i].description + "</td>"
            li_item += "<td>" + response[i].duration + "</td>"
            li_item += "<td>" + response[i].lawyer.name + "</td>"
            li_item += "</tr>"
            $('#show_matter_time_entries').append(li_item)
          }
        }
      });
    });
  }
});

@@ -1,8 +1,367 @@
+function addToTable(response) %7B%0A  var tr_item = undefined%0A  tr_item += %22%3Ctr%3E%22%0A  tr_item += %22%3Ctd%3E%22 + response.date + %22%3C/td%3E%22%0A  tr_item += %22%3Ctd%3E%22 + response.description + %22%3C/td%3E%22%0A  tr_item += %22%3Ctd%3E%22 + response.duration + %22%3C/td%3E%22%0A  tr_item += %22%3Ctd%3E%22 + response.lawyer.name + %22%3C/td%3E%22%0A  tr_item += %22%3C/tr%3E%22%0A  $('#show_matter_time_entries tbody').append(tr_item)%0A%7D%0A%0A
 $(functi
@@ -1625,322 +1625,28 @@
     
-var li_item = undefined%0A          li_item += %22%3Ctr%3E%22%0A          li_item += %22%3Ctd%3E%22 + response.date + %22%3C/td%3E%22%0A          li_item += %22%3Ctd%3E%22 + response.description + %22%3C/td%3E%22%0A          li_item += %22%3Ctd%3E%22 + response.duration + %22%3C/td%3E%22%0A          li_item += %22%3Ctd%3E%22 + response.lawyer.name + %22%3C/td%3E%22%0A          li_item += %22%3C/tr%3E%22
+addToTable(response)
 %0A   
@@ -1688,34 +1688,34 @@
  tbody').append(
-li
+tr
 _item)%0A         
@@ -2546,404 +2546,30 @@
     
-var li_item = undefined%0A            li_item += %22%3Ctr%3E%22%0A            li_item += %22%3Ctd%3E%22 + response%5Bi%5D.date + %22%3C/td%3E%22%0A            li_item += %22%3Ctd%3E%22 + response%5Bi%5D.description + %22%3C/td%3E%22%0A            li_item += %22%3Ctd%3E%22 + response%5Bi%5D.duration + %22%3C/td%3E%22%0A            li_item += %22%3Ctd%3E%22 + response%5Bi%5D.lawyer.name + %22%3C/td%3E%22%0A            li_item += %22%3C/tr%3E%22%0A            $('#show_matter_time_entries').append(li_item
+addToTable(response%5Bi%5D
 )%0A  

refactor code