How to access object properties with special characters?
P粉463824410
P粉463824410 2023-08-23 00:26:33
0
1
428
<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>
P粉463824410
P粉463824410

reply all(1)
P粉514001887

Use square bracket notation:

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

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!