First and foremost, the code is somewhat flawed, with both randomPostTitle
and firstEl
null at times, but no checks for this. I would recommend starting by refactoring the code to remove these issues. Also, there's a lot of document.querySelector()
or document.getElementById()
, which is overly broad, since you only want to apply changes in specific elements. You can use this.element.querySelector()
, this.element.getElementById()
to constrain the search to just that component. There's also a lot of unnecessary code repetition which, IMO, could be eliminated and generalized to clean up the codebase. The entire callback passsed to the extend function seems to be the same every time, except for which discussion element is being modified.