3

I have a VF page based on an object. It has a lot of lookup relationships. When a lookup is selected for a certain field (let's say Market_c), I'd like to do some processing in Javascript. How do I hook an existing apex:inputfield tag to a javascript function? And once in the Javascript function, how do I get the value of the selected Market_c.

I've tried hooking up onchange event, but it simply does not fire.

2 Answers 2

6
<script>
function doSomething() {
    // use '_lkid' suffix to get the element containing the SF Id in value
    var lkfield = document.getElementById('{!$Component.fieldValue}_lkid');
    // lkfield.value now has the selected ID value
}
</script>
<!-- intervening code ommitted -->
<apex:inputField id="fieldValue" value="{!Record.Field}">
    <apex:actionSupport event="onchange" reRender="someArea" oncomplete="doSomething()"/>
</apex:inputField>
2
  • I just tried this and it gives me JS error "Cannot read property value of null" Nov 24, 2014 at 23:00
  • seems like you have an answer for everything. No matter what problem I face, after some googling I find a similar question here at SFSE with your answer being the accepted one :)
    – Novarg
    May 22, 2017 at 14:46
0

I think our friend (above) was being illustrative rather than actual code. As a helper, this is something that might give a little more practical guidance.

<script type="text/javascript">
function doSomething(elemid) {
    // use '_lkid' suffix to get the element containing the SF Id in value
    var lkfield = document.getElementById(elemid + '_lkid').value;
    // lkfield.value now has the selected ID value
}
</script>
<!-- intervening code omitted -->
<apex:inputField id="fieldValue" value="{!Record.Field}">
    <apex:actionSupport event="onchange" reRender="someArea" oncomplete="doSomething(this.id)"/>
</apex:inputField>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .