Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access object properties containing special characters?

Tags:

javascript

I have a form DOM element:

var virDom = document.getElementsByTagName("form")[0]; 

virDom has two fields with IDs creditId and pwdId... I can access virDom.creditId without any issue, but virDom.pwdId.. is failing with a syntax error, because of the periods contained in the name.

How can I access such properties?

like image 213
user1673257 Avatar asked Oct 18 '12 11:10

user1673257


People also ask

How do you access the properties of an object with a variable?

Answer: Use the Square Bracket ( [] ) Notation There are two ways to access or get the value of a property from an object — the dot ( . ) notation, like obj. foo , and the square bracket ( [] ) notation, like obj[foo] .

How do you access the properties of objects in typescript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .


1 Answers

Use bracket notation:

virDom['creditId'] virDom['pwdId..'] 

This applies to any object, and it is particularly useful for non-identifier-safe characters and also for accessing keys that you may not know ahead of time.

like image 91
Niet the Dark Absol Avatar answered Oct 13 '22 16:10

Niet the Dark Absol