Yes you are right, that's a good approach. Another commenter linked a great article on accessibility and "card" style content which also recommended that approach, worth a read as it discusses a lot of the caveats and your approach tackles the majority of the issues you can encounter.
It’s literally 2 loc, it doesn’t affect accessibility and it’s using JavaScript for what it was meant : adding some optional convenience.
I realize I’ve not done real server rendered sites for years, but adding good old « optional convenience » JS without jQuery and broken browsers, but with ES6, modules, and maybe WebComponents would probably be a really pleasant experience.