0

My code is the following: HTML

<input type="date" min="2023-01-23" id="date-picker" class="date"> <button id="btn-calc" class="btn" onclick="button()">Calculate</button>

JS

` let dateAdd = document.getElementById("date-picker").value;

function button(){

   console.log(dateAdd);
}`

I want to store a userinput(date) in a variable, so i created the "dateAdd" let, but when i choose a date the console shows an empty row(?)

What is the secret i dont get whit this? Thanks!

A googled after it but nothing.The way i want this to work is when the user pick a date, the choosen date is get logged in to the console. The only way i got this kinda work, when i put the variable inside the button() function. But i can't get the variable outside the function.... i dontgetitpleasesendheeeelp.

Lame8666
  • 1
  • 1
  • try changing your function signature to include the event parameter: function button(e){console.log(dateAdd); use var instead of let. there is probably a scoping issue too – This Guy Jan 24 '23 at 21:49
  • 1
    Move `let dateAdd = document.getElementById("date-picker").value;` inside the `button` function – UnholySheep Jan 24 '23 at 21:49
  • Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Jan 24 '23 at 21:50

0 Answers0