1

I am overriding the Edit button to take the user to a VisualForce page that renders the record details via with inline editing enabled. I'm trying to write some Javascript (or jQuery) that selects the individual address fields on the modal that pops up when you double-click the Address field.

When I try to select the fields by id, they cannot be found. I have verified that I have the right IDs.

Are those fields in a different iframe? Why can't I select them? How should I go about selecting them?

2

If you are using VisualForce Components (e.g. apex:outputPanel), their "id" value will actually be part (the end) of a longer string that defines their id in the DOM. One quick and dirty way to find the element is to use the Ends With Selector.

<apex:outputPanel id="myElement" />
<script>
    (function ($) {
        "use strict";
        console.debug($("#myElement")); // nada
        console.debug($("[id$='myElement']")); // bingo
    })(jQuery);
</script>

You can also use style class which may be more reliable/performant.

<apex:outputPanel styleClass="myElement" />
<script>
    (function ($) {
        "use strict";
        console.debug($(".myElement"));
    })(jQuery);
</script>

Or depending on the component structure you can use the $Component Global Variable. Note that this approach will not work from a Static Resource without merging in the values from the page.

<apex:outputPanel id="myElement" />
<script>
    (function ($) {
        "use strict";
        console.debug($("{!$Component.myElement}"));
    })(jQuery);
</script>
  • Nice! The attributeEndsWith is nifty, never seen that before. $Component is perpetually frustrating when you have to recreate the component hierarchy if you have nested components. – Brian Mansfield May 18 '15 at 19:33
  • @BrianMansfield Yes I also find it rather painful and far from ideal. Probably the worst of the three options IMO. I almost never use it except for very simple pages. – Adrian Larson May 18 '15 at 19:35

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.