I am working on page where there are 5 fields in a field set . Now , I want to giv Id to a particular field in Field set and want to use $Component to get the field and run some Javascript function. Unfortunately, there is field set not the field. how can I provide ID to the field in the field set and use document.getElementById on that field to run Javascript function on it


I don't think you can control the ID values. I suggest that instead you use the class of the elements to find them.

Here is an example:

<apex:page standardController="Contact">
        <apex:repeat value="{!$ObjectType.Contact.FieldSets.Test}" var="f">
            <apex:inputField value="{!Contact[f]}" styleClass="{!f}"/>
function firstOfClass(className) {
    var els = document.getElementsByClassName(className);
    return els.length > 0 ? els[0] : null;

In this example I have a field set called "Test" defined that includes the field "AssistantName". You could include a loop counter value in the class name instead if you wanted the logic to work by index rather than field name.

The class does not have any CSS associated; it is just being used to mark the element so that the element can be found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.