0

JS beginner here. I am trying to save the date that a user will make when using the date type input element. Input successfully is successfully stored when I place the input within the eventListener function. When I try to access input outside the value I receive an error or undefined depending on how I try to fix this. How can I access the var 'input' outside the eventListener function and successfully log input within appController?

 let domStrings = {
    
    userDate : document.querySelector('#userDate')
}

let input;

appController();

function appController(){
    
    
    listen()
    
    console.log(input);
    
}

 function listen(){
      domStrings.userDate.addEventListener('change', function(){
       input = domStrings.userDate.value;

    
})
    //return input;
}
nizoom
  • 105
  • 6
  • 1
    `input` won't be set until the user changes the input. You're logging it as soon as you call `appController()`. – Barmar Sep 13 '20 at 23:31
  • Thank you. How would you advise making input accessible after the change takes place? – nizoom Sep 13 '20 at 23:33
  • 1
    It is accessible after the change takes place. You just have to wait until then to log it. – Barmar Sep 13 '20 at 23:50

1 Answers1

-2

try using Es6 syntax on function like this

 function listen(){
  domStrings.userDate.addEventListener('change', ()=>{
   input = domStrings.userDate.value;

})

Moukim hfaiedh
  • 409
  • 6
  • 9