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

Sites, elements and element iDs

Hey all, 


We're in the middle of doing our best to roll out a production site. Each site page has a number of elements that gather display or gather data, including static resources and Apex forms.


Of course, many of our data fields are acted upon by Javascript that validate certain things prior to submit, or that perform operations not handled by Apex, prior to submit.


We're hitting a big challenge, which is that each element is assigned an ID, but that IDs are NOT static; they seem to depend upon the order that the element falls within the page. For instance, our form has the following name, now:



<form id="j_id0:CT_App_Template:j_id10" ..../>


 but had a different id (13) earlier, and still a different id prior to that.  These changing IDs make for very confusing and bug-prone development, especially because there are quite a few elements on each page, and the number of elements changes as we add or subtract visual elements (referred to as static resources).


This is causing us to push static resources to other servers, for instance, hosting on internal sites that we can refer to directly, rather than dealing with the issue of changing IDs.


Has anyone run into & found a solution to this aggravating issue, and/or is there a way to catch and dynamically assign the ID, or tell a resource to always use the SAME id,  on a consistent basis?


Thanks for your support!



Best Answer chosen by Admin (Salesforce Developers) 
Nathan CrosbyNathan Crosby

Take a look at using the $Component global variable in your Visualforce pages. Providing you give each element in the hierarchy an id you can identify each element using this variable.




var strCity = document.getElementById('{!$Component.theForm.accountSearch.s2.s2t.acctCity}').value;


Where acctCity is the id for an inputText component nested within a form, pageBlock, pageBlockSection, pageBlockSectionItem.