Skip to content
Advertisement

How to change the value of a global variable when a user select an option

i have the html below where i let the user choose between two options

JavaScript

After that i initialize a global variable that gets the “gr” value. I want to change the global value based on what the user chooses from the drop down menu (gr or en). On change selection event, i tried the code below but the variable Language only changes inside the function but outside it remains unchanged (Language = “gr”;)

JavaScript

Advertisement

Answer

You need to update all of your global variables after you change the language the lines that use the Language variable do not auto rerun when you update the variables so you need to update them. So you would need to add the title and title2 lines inside of your function.

JavaScript

How I would do it

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement