Javascript help: catch changes to specific field

Hi all,

I'm trying to have a little Javascript snippet run every time a specific field is updated with an inline edit. I found the exact trigger criteria was looking for on Knack's developer documentation - but I'm running into an issue where the code won't 'reset' once its been run once without refreshing the whole page. Basically it works great the first time - if field_yy is updated it runs the code. But once it's been run once, the "//do something such as.." section will run on any inline update to the table, whereas I want it to only run if field_yy is updated again.

is there any code that would somehow accomplish this without doing a full location.reload()?

Any help would be greatly appreciated!

Catch changes to a specific field

If you want to trigger a certain action - for instance, an API call to make a follow-up change to a record - only when a certain field is changed, you can do so by:

  1. Nesting a knack-cell-update or knack-record-update listener inside a knack-records-render listener
  2. Identifying the record which was updated (if we're tracking an inline update in a table) by checking against the updated record's ID
  3. Comparing the value of the field(s) in question between the pre- and post-update versions of the record
  4. Taking an action of our choosing if the values are not identical

// Replace view_xx with the view key for the table view in question
// Replace field_yy with the field key for the field whose changes you want to track
$(document).on('knack-records-render.view_xx', function(event, scene, records) {
  $(document).on('knack-cell-update.view_xx', function(event, view, updatedRecord) {
    // Filter the initially loaded records for the one with the same ID as the updated one
    var recordBeforeUpdate = records.filter(recordFromRenderEvent => {
      return recordFromRenderEvent.id === updatedRecord.id;
    })[0];
<span class="hljs-keyword">if</span> (updatedRecord.field_yy !== recordBeforeUpdate.field_yy) {
  <span class="hljs-comment">// Do something, such as:</span>
  alert(<span class="hljs-string">'Field_yy has been changed!'</span>)
}

});
});