0

I'm trying to do something like this:

<html>
<body>
 <input id="input" type="text">
 <input type="button" onclick="a()" value="submit">
 <script>
  function a(){
    x=document.getElementById("input").value;
    b(x)
  }
  function b(userInput){
    var z=userInput
  }
 </script>
</body>
<html>

I want the name of "z" to change every time the functions runs, so I can get a new variable every time the user enters a new input.

Leon Deer
  • 27
  • 2
  • 2
    Every time you call `b(…)`, a new variable with the name `z` *will* be instantiated in the local scope of that call. – Bergi Nov 26 '19 at 23:28
  • 2
    Hi Leon, I don't think it's very clear what you are asking. Do you want to keep track of all the user inputs? If this is what you want you should look into arrays. If the variable z is defined outside of the function then you will be able to add the new user input to it after each button click – KNejad Nov 26 '19 at 23:30
  • 1
    What you ask is possible in global level `window['var1']` `window['var2']` but itis strange and bad pattern. Can you explain what do you want to accomplish? Most likely, a single variable or an array will do the trick. – Raúl Martín Nov 26 '19 at 23:31
  • I'm studying now javascript and trying different things to get all the ideas, I bumped into this issue, so just want to know if there is a basic way of solving it? – Leon Deer Nov 26 '19 at 23:36
  • You want z to change each time, or you want it to be an array that contains all the user inputs? – Dr_Derp Nov 27 '19 at 01:02
  • What's the point of doing this anyways? Seems like a poor design concept. – StackSlave Nov 27 '19 at 03:07

1 Answers1

0

Like this? If so - try next time to be cleaner with ur question.

<input id="input" type="text" placeholder="type text here">

<input type="button" onclick="submit()" value="submit">
<pre id="result"></pre>

<script>
  const values = [];

  function submit() {
    values.push(document.getElementById("input").value);
    console.log(values, values[values.length - 1]);
    document.getElementById("result").innerHTML = values.join(' ');
  }
</script>
sonnenhaft
  • 1,638
  • 1
  • 13
  • 15