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
Salesforce 283Salesforce 283 

When we click the button the name will be displayed in outputpanel with color Using Javascript

I am new to the Javascript with salesforce. 

<apex:page controller="changecolorusingjs"  >
<script>
function onclick()
{
  var x,text; 
   x=document.getElementById('out').value; 
    x.style.color="blue";  
     if(x!=null)
      {
         name="acc.name";
      }
   
   document.getElementById('ou').innerHTML=name;
 }    
    </script>

 <apex:form id="d" >
  <apex:pageBlock >
  <apex:pageBlockSection id="out" >
    <apex:inputField value="{!acc.name}" required="false"/>
    <apex:inputField value="{!acc.phone}"/>
    <apex:commandButton value="click" action="{!click}" onclick="onclick" reRender="d"/>
  </apex:pageBlockSection>
  <apex:outputPanel id="ou">
  
  </apex:outputPanel>
  </apex:pageBlock>
 </apex:form>
</apex:page>
Best Answer chosen by Salesforce 283
Darshan Shah2Darshan Shah2
Hello salesforce developer 31,

We have written ids to every component so we can easily traverse to any component in javascript.

In our visualforce page 'onclick' event fires first then action method will fire. If we write 'return false' then action method will not fire.

to give blue color to text in javascript rewrite code as below:

<script>
    function showName()
    {
        var text;
        var x = document.getElementById("pgAccount:frm:pbAccount:out:ifName").value;
        document.getElementById("pgAccount:frm:pbAccount:ou").innerHTML=x;
        document.getElementById("pgAccount:frm:pbAccount:ou").style.color = "blue";
     }    
    </script>

If it helps you then mark question as 'SOLVED'.

Warm Regards,
Darshan Shah

All Answers

Darshan Shah2Darshan Shah2
Hi salesforce developer 31,

Just make small changes as shown below:
Kindly let me know if it helps you.

<apex:page Controller="changecolorusingjs" id="pgAccount">
  <script>
    function showName()
    {
        var text;
        var x = document.getElementById("pgAccount:frm:pbAccount:out:ifName").value;
        x.fontcolor("blue");
        document.getElementById("pgAccount:frm:pbAccount:ou").innerHTML=x;
     }    
    </script>

 <apex:form id="frm" >
  <apex:pageBlock id="pbAccount">
  <apex:pageBlockSection id="out" >
    <apex:inputField value="{!acc.name}" required="false" id="ifName"/>
    <apex:inputField value="{!acc.phone}" id="ifPhone"/>
    <apex:commandButton value="click" action="{!click}" onclick="showName();return true;" reRender="ou"/>
  </apex:pageBlockSection>
  <apex:outputPanel id="ou">
  </apex:outputPanel>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Warm Regards,
Darshan Shah
Salesforce 283Salesforce 283
Hi Darshan, Its not working...
Salesforce 283Salesforce 283
Hi Darshan, Its Working... How it works? Why we have written ids in every components and return true.
Darshan Shah2Darshan Shah2
Hello salesforce developer 31,

We have written ids to every component so we can easily traverse to any component in javascript.

In our visualforce page 'onclick' event fires first then action method will fire. If we write 'return false' then action method will not fire.

to give blue color to text in javascript rewrite code as below:

<script>
    function showName()
    {
        var text;
        var x = document.getElementById("pgAccount:frm:pbAccount:out:ifName").value;
        document.getElementById("pgAccount:frm:pbAccount:ou").innerHTML=x;
        document.getElementById("pgAccount:frm:pbAccount:ou").style.color = "blue";
     }    
    </script>

If it helps you then mark question as 'SOLVED'.

Warm Regards,
Darshan Shah
This was selected as the best answer
Darshan Shah2Darshan Shah2
Hi Salesforce Developer 31,

Let me know whether above answers solved your problem or not so I can help you.

If code is working fine then mark this question as solved so other people can get help from this.

Regards,
Darshan