icantbebothered is right. You can't run javascript code in mobi books out-of-the-box.
Custom Dialog instead (made of javascript+html) works for your purpose.
You can find some examples looking through the code of the following apps:
Kindle Menu,
KindleGallery or
KindleNotes.
Another way to use javascript, inside a book (instead of a custom dialog app), is using mesquite. I never did it, so you need to search for it on your own, using the forum search tools.
Here you can find some nice javascript system commands, working on both custom dialogs and mesquite apps:
https://www.mobileread.com/forums/sho...d.php?t=210280
P.S. all what I'm saying here regards only Kindle Touch and Kindle PW devices