Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: how to access property whose name is dynamic? [duplicate]

Tags:

javascript

Possible Duplicate:
Javascript create variable from its name

The code below checks to see if the javascript object form_errors has the property whose name is specified by this.name, where this refers to a text input

if (form_errors.hasOwnProperty(this.name)) {
  alert(form_errors.<this.name>;
}

How can I access the property without hard-coding the property name but leaving in the generalized form this.name ? Thanks.

like image 209
tamakisquare Avatar asked Mar 23 '26 09:03

tamakisquare


1 Answers

Use brackets:

form_errors[this.name]

You can access any property of an object by passing in a string with its name. For instance, foo.bar and foo['bar'] have the same result.

like image 185
zneak Avatar answered Mar 24 '26 23:03

zneak