0

I'm trying to pass a variable weekendingdate to an HTML function to create a popup but somehow it doesn't work:

<!DOCTYPE html>
<html>
  <h2>Select Week Ending date  </h2>  
  <br /> 
  <input type="date" name="Weekendingdate">
  <button onclick="myFunction()">Try it</button>
  <br />  
  <script>
    function myFunction() {
      alert(Weekendingdate);
    }
   </script>
</html>

1 Answer 1

1

You need to either pass the value of the input as an argument to the function - or get the value within the function. Note that since you have the input type="date" - you can set the values of the date portions individually - or use the built in date picker. Its also worth noting that all the browsers treat inputs with type="date" differently - so you may want to alter that approach.

Anyway - the flow is - change set the date in the date field - then click the button and you will get your alert giving the date (value of the input) that you entered.

Also - one other thing - in dev, its better to console.log() your results for testing / debugging. I left it as the alert you had, but wanted to change it :)

function myFunction() {
  let Weekendingdate = document.getElementsByName('Weekendingdate')[0].value;
  alert(Weekendingdate);
}
<h2>Select Week Ending date  </h2>  
<input type="date" name="Weekendingdate">
<button onclick="myFunction()">Try it</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.