You need to sign in to do that
Don't have an account?
Java script function does not read Apex lookup field values
Hi,
I have a VF page and a controller for this page. The controller uses an instance of one of my custom objects. This custom has a look-up field which I am showing on my VF page. Below is my VF page code. When I click on Add I expect the Javascript function to show the value entered in the look-up field but there is no output. I am missing something...could you please help me understand why my function is not displaying the entered value.
Thanks in advance.
Code-
<apex:page controller="TripLegsGroup" >
<script>
function AddCity() {
alert('Entered');
var mct = document.getElementById('{!$Component.mForm.mCity}').value;
alert (mct);
}
</script>
<apex:pageblock title="Group book legs">
<apex:form id="mForm" >
<apex:pageBlock title="Enter trip leg details">
<apex:pageblockSection columns="2">
<apex:pageblockSectionItem >
<apex:outputLabel value="Travelling to:"/>
<apex:outputPanel >
<apex:inputField id="mCity" value="{!GroupLeg.To_City__c}" /> <-- This is look-up field, I would like to display the entered value
<apex:commandButton value="Add" onkeypress="return AddCity()"/> <-- This command button click calls JS function
</apex:outputPanel>
</apex:pageblockSectionItem>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:pageblock>
</apex:page>
Command buttons have submit behaviour by default. your JS function is reading an element on the page. Try using simple
<input type="button" tag... instead of commandbutton.
Try this changes
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.