Find your content:

Search form

You are here

Visualforce component use custom id attribute

 
Share

Any apex visualforce component created with a custom id, results into an html component created with an id which is prefixed by the their parent's id.

<apex:outputPanel layout="block" id="hello" styleClass="helloClass">

This results into a div tag that results with the prefixed id.

<div id="j_id0:hello" class="helloClass">

Is there a way that the parent id j_id0 can be avoided to prefix with the id of the class. This way the resulting component may look like this.

<div id="hello" class="helloClass">


Attribution to: Nitin Kundapur Bhat

Possible Suggestion/Solution #1

Controlling the Id can be achieved but no precisely as you mentioning .But there is a way to get Id populated as you want in Visualforce Page.

One way to do the same will be assigning Ids to All Compoenents on Page

Official Document link

<apex:page id="thePage">

<apex:form id="theForm">
    <apex:pageBlock id="thePageBlock" title="Targeting IDs with $Component">
        <apex:pageBlockSection id="theSection">
            <apex:pageBlockSectionItem id="theSectionItem">
                All the alerts refer to this component.

                <p>The full DOM ID resembles something like this:<br/>
                thePage:theForm:thePageBlock:theSection:theSectionItem</p>
            </apex:pageBlockSectionItem>

            <!-- Works because this outputPanel has a parent in common 
                 with "theSectionItem" component -->
            <apex:outputPanel layout="block" styleClass="clicker"
                onclick="alert('{!$Component.theSectionItem}');">
                First click here
            </apex:outputPanel>
        </apex:pageBlockSection>

        <apex:pageBlockButtons id="theButtons" location="bottom">
            <!-- Works because this outputPanel has a grandparent ("theSection")
                 in common with "theSectionItem" -->
            <apex:outputPanel layout="block" styleClass="clicker"
                onclick="alert('{!$Component.theSection.theSectionItem}');">
                Second click here
            </apex:outputPanel>

            <!-- Works because this outputPanel has a distant ancestor ("theForm")
                 in common with "theSectionItem" -->
            <apex:outputPanel layout="block" styleClass="clicker"
                onclick="alert('
                {!$Component.theForm.thePageBlock.theSection.theSectionItem}');">
                Third click here
            </apex:outputPanel>
        </apex:pageBlockButtons>

    </apex:pageBlock>

    <!-- Works because this outputPanel is a sibling to "thePageBlock",
         and specifies the complete ID path from that sibling -->
    <apex:outputPanel layout="block" styleClass="clicker"
        onclick="alert('{!$Component.thePageBlock.theSection.theSectionItem}');">
        Fourth click here
    </apex:outputPanel>

    <hr/>

    <!-- Won't work because this outputPanel doesn't provide a path 
         that includes a sibling or common ancestor -->
    <apex:outputPanel layout="block" styleClass="clicker"
        onclick="alert('{!$Component.theSection.theSectionItem}');">
        This won't work
    </apex:outputPanel>

    <!-- Won't work because this outputPanel doesn't provide a path 
         that includes a sibling or common ancestor -->
    <apex:outputPanel layout="block" styleClass="clicker"
        onclick="alert('{!$Component.theSectionItem}');">
        Won't work either
    </apex:outputPanel>

</apex:form>

Basically once you provide the ID for the Page and other sections the DOM will follow specific pattern which can be easily predicted and wont change across instances .


Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34420

My Block Status

My Block Content