let selectElement = document.querySelector('#gammeselect'); let turnintonumber; function pick() { var result = selectElement.options[selectElement.selectedIndex].value; turnintonumber = parseInt(result); return turnintonumber; } selectElement.addEventListener ('change', pick); //I've tried here several declarations of variables declarations or console.log to pick the result of 'turnintonumber'. Hello, I have created a function to retrieve a value chosen by the user and convert it into a number. I tested my 'pick()' function inside it using a 'console.log(turnintonumber)' and a 'console.log(typeof turnintonumber)'. But I can't retrieve the value of 'turnintonumber' outside of the 'pick()' function. Thank you for your help. When I display 'turnintonumber' in my browser console, it returns the value 'undefined'.
Asked
Active
Viewed 22 times
0
-
The `turnintonumber` variable is initially declared outside of the pick() function, but its value is assigned inside the pick() function based on the selected option of a dropdown menu. pick() function is triggered by the change event of the dropdown menu the value of turnintonumber is only updated when the user interacts with the dropdown menu. If you're trying to access `turnintonumber` outside of the pick() function before the user has interacted with the dropdown menu, it will still hold its initial value of undefined. Updated code example: https://codesandbox.io/s/select-option-3bn0gu – Rohit Chugh Apr 19 '23 at 09:42