0

How do I use a dynamic parameter name when accessing a variable?

Example:

opener.document.form.namerow_3.value  = this.cells[0].innerHTML;
opener.window.form.{varaible}.value=this.cells[0].innerHTML;

In this case, the variable would be namerow_3, which will change based on user selection.

How do I solve the problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
Ajay06
  • 3
  • 1
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – thmsdnnr Jul 25 '19 at 03:14

1 Answers1

0

If I understand your question correctly, you're trying to access a dynamic property of the form object. You can do that by accessing the object like this:

// Base example of how to access namerow_3
opener.document.form.namerow_3.value  = this.cells[0].innerHTML;

// Example with static accessor name
opener.document.form["namerow_3"].value = this.cells[0].innerHTML;

// Example with variable
var propName = "namerow_3";
opener.document.form[propName].value = this.cells[0].innerHTML;

Since most regular objects in JavaScript are basically a hashmap, you can usually access an objects property by specifying it's key like you would an array's index (in this case, form["namerow_3"]).

Joseph
  • 865
  • 5
  • 20