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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With