I’m trying to apply an event listener to a class of multiline inputs. I have installed the classify plugin and added a class to the relevant inputs. My plugin has a field called listenerClass, where I can put in the class name of the multilines. Right now, I have added this event listener into the update function. It works perfectly except it always runs twice. I guess I could add a counter to only run every second time, but I’m not sure how reliable that would be. Does anyone have any suggestions, so that I only trigger an event once?
I can’t access the properties object from the initialize function. For my case, I have multiple classes with the event listener, so I need a field on the element where I can add the class.
I did try another method where I added the event listener to the element ID (as opposed to a class) using the initialize function. However, if I deleted a row in my repeating group and later added a new row, the instance from the previous row remained, so the event listener wouldn’t run again. I mention the issue in more depth here:
I have multiple multiline inputs on the page. I want to apply the event listener to two sets of multiline inputs, while the third set I want to behave normally (i.e. I don’t want to apply the event listener). So, I can’t use a selector like $("textarea") because it will apply to all the multiline inputs on the page. I need a way to distinguish between multiline inputs, which is why I opted for classes. The only other method I came up with is assigning a listener to each ID attribute, but that seemed more convoluted.
Regardless of where the listener gets attached, the solution is to simply not execute it twice. In initialize, initialize the initialized state (how many times can you use the same word in one sentence, one wonders):
instance.data.initialized = null (or false, your pick, same thing)
Then, in update, do a bunch of stuff and then, at the very end, set: