function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dev_forcedev_force 

Accessing Components with their IDs - how does it work?

Anyone know why $Component is useful?

 

For example, what is really the difference between  

onclick="alert('{!$Component.theSection.theSectionItem}');

 

and

onclick="alert('detailPage:mainForm:pageBlock:hiddenField');

 

 

... there doesn't seem to be any validation on the $Component  tag.

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Because $Component is protection against changes in DOM id creation.  DOM ids can and do change, especially when the structure of your page changes.

 

We don't promise that the way DOM ids are created, based on the component ids you pass into your id attributes, will be the same forever, so that's why you have a safe mechanism to access your components by id.

 

Often times components that probably should have been contributing to the resulting DOM id were not, and we have made changes to this before.  By using $Component correctly, your script should never break (unless you drastically rearrange your page, of course).