0

I am trying to dynamically assign variable names using the user's input. For example:

var input = document.querySelector('input');

for(var i = 0; i < 10; i++){
   var newVariableName = //input.value;
}

Any help would be very much appreciated.

Thank you, Scratch Cat

6
  • Can you please elaborate Commented Jun 6, 2017 at 11:49
  • What would you like to do with it? Theoretically you could do it by using the global object, though it might be easier to have a dictionary object that matches the given names and values Commented Jun 6, 2017 at 11:50
  • 3
    Why don't you just use an array/object? Commented Jun 6, 2017 at 11:50
  • 3
    You can add new properties to the window object with dynamic names: window[dynamicallyGeneratedString] = input.value; Commented Jun 6, 2017 at 11:50
  • 1
    Possible duplicate of Use dynamic variable names in JavaScript Commented Jun 6, 2017 at 11:56

2 Answers 2

1

Everything in JavaScript is an object. The way JavaScript works, you can add properties to objects in two ways:

  1. Specify them the fixed way (e.g. obj.propertyName = 'Value')
  2. Specify them using array notation (e.g. obj[propertyName] = 'Value'). In this case, note that propertyName is a string value.

In both cases, the result will be exactly the same. You could retrieve those properties likewise, e.g. obj.propertyName and obj[propertyName]. Both will return 'Value'.

In your case, @LuudJacobs's suggestion about using the window object will most probably do the trick...

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

2 Comments

Yes but you can't have the same name for different objects in an array. It crashes.
Oh I think I understand now Thank you very much.
0

You can use array in which the keys will be the input values and the value would be anything you want.

html

<form>

<input type="text" value="demo1" class="example"><br />
<input type="text" value="demo2" class="example"><br />
<input type="text" value="demo3" class="example">

</form>

js First you declare array

var a = new Array();

Then you use for loop to assign key names to array which will be the input values

for(var i = 0; i < 3; i++){
    a[x[i].value] = x[i].value;
}

Finally you can use those key names to access the values

alert(a['demo1'] + ' ' +a['demo2'] + ' ' + a['demo3']);

Here is a link to an example https://jsfiddle.net/309fpsjn/1/

<html>
<form>

<input type="text" value="demo1" class="example"><br />
<input type="text" value="demo2" class="example"><br />
<input type="text" value="demo3" class="example">

</form>
<script>
var x = document.querySelectorAll(".example");

var a = new Array();

for(var i = 0; i < 3; i++){
    a[x[i].value] = x[i].value;
}

alert(a['demo1'] + ' ' +a['demo2'] + ' ' + a['demo3']);

</script>
</html>

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.