Okay, I was able to fix my problem by doing the following.
Change:
$id = uniqid('button');
To
$id = str_replace('.','-',uniqid('button',true));
That includes an extra number appended with a dot. Which creates invalid DOM IDs (in my case) so I just replace it with a dash.