To access and use a variable or function dinamically, with Name from a String in JavaScript, just use the window object, with this syntax:
window["variable_name"]

// For function
window["function_name"](parameters)
Here is some examples, see the explanations in code.
- Click on the code to select it.

1. Using variable with name stored into a string.
<script>
var vr_name = 'vr1';    // string with name of a variable

var vr1 = 'coursesweb.net/';

// uses variable with name stored as string in vr_name
// window[vr_name] /window["vr1"] is the value of vr1
var site = 'http://'+ window[vr_name];

// Test
alert(site);    // https://coursesweb.net/
</script>

2. Using variable dinamically, with name stored into a string in object.
<script>
// object with strings with variable names
var vr_name = {'v1':'vr1', 'v2':'vr2'};
var vr1 = 'coursesweb.net/';
var vr2 = 'google.com';

// uses variable dinamically, with name stored as string in vr_name
var i = 2;
var site = 'http://'+ window[vr_name['v'+ i]];

// Test
alert(site);    // http://google.com/
</script>

3. Calling function with name stored into a string.
<script>
function f1(a, b) {
  return a + b;
}

var f_name = 'f1';    // string with function name

// uses function with name stored as string in f_name
// window[f_name](parameters) calls the f1(parameters)
var sum = window[f_name](12, 23);

// Test
alert(sum);    // 35
</script>

4. Calling an object method with name stored in string into an object (use: object["method_name"](parameters)).
<script>
// object with strings with method names
var methods = {'m1':'hi1', 'm2':'hi2'};

// object with two methods
var obj = {
  'hi1': function(name) {
    return 'Hello dear '+ name;
  },
  'hi2': function(name) {
    return 'Hola '+ name;
  }
};

// uses object method with name stored as string in methods
var hi = obj[methods['m1']]('ME');

// Test
alert(hi);    // Hello dear ME
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Using Variable and Function with Name from String in JavaScript

Last accessed pages

  1. HTTP Errors and Logging (39)
  2. Add, Change, and Remove Attributes with jQuery (35999)
  3. Select in MySQL, Output results in HTML Table (3789)
  4. jQuery Drag and Drop Rows between two similar Tables (6875)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (8371)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (443)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (351)
  3. Register and show online users and visitors (319)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (300)
  5. Insert, Select and Update NULL value in MySQL (214)