Vanilla javascript in custom gutenberg block not working

The save function of blocks only saves markup to the database (along with HTML comments, which WP uses to parse the HTML back into the Editor). To use JS on the front end with your block, you will need to create a separate JS file for what needs to run there, and enqueue it so WordPress knows to include it.