function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramon PereiraRamon Pereira 

Java Script in VisualForce

Hi, 

 

I would like to copy the value from one field to another automatically.
I made the following code, but it does not run.
Someone must help me?

 

<apex:page standardController="Opportunity" id="page1" >

<apex:sectionHeader title="Test"/>
<apex:messages />
<apex:form id="form1">
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" title="Salvar" value="Salve"  />
<apex:commandButton action="{!cancel}" title="Cancelar" value="Cancel" />
</apex:pageBlockButtons>



<apex:panelGrid columns="2">
<apex:outputLabel >Client:</apex:outputLabel>
<apex:inputField value="{!Opportunity.AccountId}" required="true" id="campo2" />
<apex:outputLabel > Name: </apex:outputLabel>
<apex:inputField value="{!Opportunity.name}" id="campo1" onblur="copyT()" />
<apex:outputLabel > Stage: </apex:outputLabel>
<apex:inputField value="{!Opportunity.StageName}" />
<apex:outputLabel > Date: </apex:outputLabel>
<apex:inputField value="{!Opportunity.CloseDate}" />

</apex:panelGrid>

</apex:pageBlock>
</apex:form>  

<script type="text/javascript">
        function copyT(){
                valor = document.getElementById('{!$Component.campo1}').value;
                
                    document.getElementById('{!$Component.campo2}').value = valor;
            }
           
    </script>

  
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Thomas DvornikThomas Dvornik

You need to chain the ids. Below is a simplified example:

 

<apex:page standardController="Opportunity">
  <apex:form id="myForm">
    <apex:pageBlock id="myPageBlock">

      <apex:panelGrid columns="2">
        <apex:inputField value="{!Opportunity.AccountId}" id="myField" />
      </apex:panelGrid>

    </apex:pageBlock>
  </apex:form>  

  <script type="text/javascript">
    Sfdc.onReady(function() {
      var id = '{!$Component.myForm.myPageBlock.myField}';
      var inputField = document.getElementById(id);
      console.log(inputField.value);
    });
  </script>
</apex:page>

 

All Answers

AdrianCCAdrianCC

Hello,

 

First of all check the error console to see if you don't have errors. I highly recommend using Firebug on Firefox(an addon)... Also check Development Mode and Show View State on the Setup > Personal Information page.

Second, I think you need to rerender the page to see the changes.

Add an actionsupport to the input element.

<apex:inputField value="{!Opportunity.name}" id="campo1" >
    <apex:actionSupport onblur="copyT()" rerender="form1" />
</apex:inputField>

 

 Have a great day!

Adrian

 

 

Ramon PereiraRamon Pereira

Hi,

Actually I need a way to capture the id generated by the sales force.
I used the sixtaxe ('{!} $ Component.campo1') to capture the id of the component, but to analyze the generated HTML it is not generating the component ID.
My need is to find the component ID.

thank you

Thomas DvornikThomas Dvornik

You need to chain the ids. Below is a simplified example:

 

<apex:page standardController="Opportunity">
  <apex:form id="myForm">
    <apex:pageBlock id="myPageBlock">

      <apex:panelGrid columns="2">
        <apex:inputField value="{!Opportunity.AccountId}" id="myField" />
      </apex:panelGrid>

    </apex:pageBlock>
  </apex:form>  

  <script type="text/javascript">
    Sfdc.onReady(function() {
      var id = '{!$Component.myForm.myPageBlock.myField}';
      var inputField = document.getElementById(id);
      console.log(inputField.value);
    });
  </script>
</apex:page>

 

This was selected as the best answer