Did you know that a JavaScript hash can be accessed using either dot notation or array notation? When dealing with a literal name, you can use dot notation like this: this.$.some_id
. However, if there is an indirection involved, such as setting this.id = 'some_id'
, then array notation is needed to find the value: this.$[this.id]
.
One thing to keep in mind is that Polymer populates the $
array only after stamping the template, which occurs before the ready
event. If you have an external binding to this.id
, accessing it through this.$.[this.id]
might not work as expected because by the time ready
is triggered, it's too late for the $
convenience.
In such situations, it's recommended to query the shadowRoot directly like this:
this.shadowRoot.querySelector('#' + this.id)
A helpful tip is to install a named div that can be easily queried against, especially if a subclass introduces a new template where this.shadowRoot
points to the updated version. For example, you can use:
this.$.id_div.querySelector('#' + this.id')
.