0

This is my code:

var global;
document.getElementById("Updatebtn").addEventListener("click", myFunction);

function myFunction() {
 var x = document.getElementById("start");
 var defaultVal = x.defaultValue;
 var currentVal = x.value;
 global = currentVal;
 return(global)
 }

console.log(global)

Html:

<input type="date" id="start" name="trip-start" value="2022-09-09" 
min="2022-09-09" max="2022-12-31"></input> 
<button class="ul-label" type="button" id = "Updatebtn"> Update 
</button><br><br>

I tried without return too but no hope, all I need is to update and use the variable outside the function everytime the button is clicked

I tried using Async based on one of the posts but I am confused on how Async works, as it doesn't react to the button input:

document.getElementById("Updatebtn").addEventListener("click", delay);


async function delay() {
  var x = document.getElementById("start");
  // `delay` returns a promise
  return new Promise(function(resolve, reject) {

  // Only `delay` is able to resolve or reject the promise
  setTimeout(function() {
  var defaultVal = x.defaultValue;
  var currentVal = x.value;
  resolve(currentVal); // After 3 seconds, resolve the promise with 
  value 42
  }, 3000);
  });
  }

  delay()
   .then(function(v) { // `delay` returns a promise

   console.log(v); // Log the value once it is resolved
  })
   .catch(function(v) {
    // Or do something else if it is rejected
    // (it would not happen in this example, since `reject` is not 
    called).
    });
  • 1
    Where do you suppose the return value would be received from a click event that may or may not take place? Event handlers generally don't have return values. – Scott Marcus Sep 13 '22 at 17:41
  • 2
    any relevant html plz? – Chris G Sep 13 '22 at 17:42
  • You just need to declare `global` outside of the function and then you can access it from within the function. – Scott Marcus Sep 13 '22 at 17:42
  • 1
    Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Ivar Sep 13 '22 at 17:43
  • @ScottMarcus I already defined it outside but still it doesn't give me the value only undefined – Abdulrahman Sep 13 '22 at 18:09
  • @Ivar But I am not using ajax, jquery or async at all – Abdulrahman Sep 13 '22 at 18:10
  • 2
    @Abdulrahman Yes you do. `myFunction` is called when the element is clicked. `console.log(global)` doesn't wait for that. So `myFunction` is called asynchronously. – Ivar Sep 13 '22 at 18:21
  • @Ivar I tried to follow the async (see the edited post), but something isn't right – Abdulrahman Sep 13 '22 at 20:36
  • 1
    Why are you not just calling the thing that needs the value. There is no need for promises and awaits when you are waiting for a user's interaction with an element. – epascarello Sep 13 '22 at 20:43

0 Answers0