I'm working on a Bootstrap website that has light and dark mode, functioning through JavaScript. And I've integrated Google reCAPTCHA (v2) (I'm not a robot) into my project successfully.
But I want to be able to change reCAPTCHA theme dynamically through the theme toggle switch. As the documentation has an attribute data-theme which we can set value to dark or light.
The main div of reCAPTCHA container through which the theme could be toggled by changing class (rc-anchor-light / rc-anchor-dark) is located inside an iframe.
I tried by adding and removing classes (rc-anchor-light / rc-anchor-dark) from the theme switch JavaScript function to change the reCAPTCHA theme dynamically, but it's not working and I'm not able to figure out why.
While reCAPTCHA v2 doesn't natively support dynamic theme switching, here's a workaround using multiple reCAPTCHA containers and JavaScript:
HTML structure for two reCAPTCHA containers with different themes
JavaScript: