0

A function may return a value; In this usage, how can this function be re-written to return the value of the user option('chosenOption', when an change event is called) and bind the value to currentLevel. Right now the currentLevel will return undefined to the console when the user choose an option, but we need it to return to the console the value of the chosen option.

let optionValues = document.querySelector('#dlevel');  

function startGame(){
  let chosenOption = optionValues.value;
    return chosenOption;
}

let CurrentLevel = optionValues.addEventListener('change', startGame)
console.log(CurrentLevel);
<select name="" id="dlevel">
        <option value="2">Avance</option>
        <option value="3">Intermediate</option>
        <option value="5">Beginner</option>
    </select>
ROOT
  • 11,363
  • 5
  • 30
  • 45
  • 1
    The way you bind it `return this.value` should do it. No need for queryselectors. Yet you nowhere output that acutal return. You merely log the return of `addEventListener()`. – Lain Jan 14 '20 at 09:15
  • 3
    [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) always returns `undefined` – Seblor Jan 14 '20 at 09:16
  • 2
    Sounds like an XY problem: why do you need to return CurrentLevel from the event listener? The event listener's callback is only invoked when an event is detected. What you want is to console log within the callback instead. – Terry Jan 14 '20 at 09:18

0 Answers0