improve javascript code

now the button will display the option you chose
reduse the repeated part of javascript code