Hi,
I'm not going to dig into your code but I believe this might be a case of having duplicate id values in your code and perhaps this is why JS won't execute correctly if and when duplicates exist. So, I suggest you check what attribute is being used to call for the "close/cancel" command. Hope...