How To Change Variable Name Within Javascript Code
- Forums
- Javascipt
- How To Change Variable Name Within Javascript Code
this is a way to use dynamically variables in javascript i show you in a simple way very basic how to use it [831], Last Updated: Sat May 18, 2024
bulan
Tue Oct 11, 2011
0 Comments
541 Visits
hi, im not sure if this is poosible. but lets say i have a variable in javascript called MyString. this is how my code looks like:
var MyString = "Hi ! ";
no i want to change the name of my string to lets say MyStringLonger so i would simply enter:
var MyStringLonger = "Hello! ";
ok, but i want to use javascript dynamic to create the variable MyStringLonger instead of me having to enter it in hard code.
ANSWER: ok, update, i found the answer. you can use the eval function. just like php, you can use it also in javascript so in my example, i would just enter my javascript code like this:
var MyString = "MyOther";
var Long = "Long";
eval(MyString + Long + "= 'Hello!'");
alert (MyOtherLong); // POPUP SHOWS MESSAGE: Hello!