Hello, I have this code to get a parameter for the matching selector:
if (_.isEmpty(fileType) || fileType.trim() === '') {
trackingElement = trackingElement.closest('[data-tracking-filetype]');
if (trackingElement) {
fileType = trackingElement.dataset.trackingFiletype;
}
}
Unfortunately, not always such element in DOM exists and then I am getting a following error in the console:
Uncaught TypeError: can't access property "closest", trackingElement is null
What I have tried:
I have tried to use the querySelector() instead of closest() method.
Also tried to use optional chaining, which got rid of the error in the console BUT caused the fileType value to be undefined, whereas in the past it has been working (save for the error in the console):
if (_.isEmpty(fileType) || fileType.trim() === '') {
trackingElement = trackingElement?.closest('[data-tracking-filetype]');
if (trackingElement) {
fileType = trackingElement.dataset.trackingFiletype;
}
}
Anyone knows how to fix it?