How to access object properties with special characters?
P粉463824410
2023-08-23 00:26:33
<p>I have a <code>form</code> DOM element: </p>
<pre class="brush:php;toolbar:false;">var virDom = document.getElementsByTagName("form")[0];</pre>
<p><code>virDom</code> has two fields with IDs, namely <code>creditId</code> and <code>pwdId..</code>. I can access <code>virDom.creditId</code> without issue, but <code>virDom.pwdId..</code> causes a syntax error due to a period in the name. </p>
<p>How do I access a property like this? </p>
Use square bracket notation:
This works for any object and is particularly useful for non-identifier safe characters and for accessing keys that may not be known in advance.