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

Use of Javascript function in Visualforce page



I have variable in my contoller it is Double Data type variable which will have values like 56.789.

I want to round-off these values using javascript and display this variable value on my visualforce page.For this  I can use toFixed or toPrecision function available in Javascript.


Can anyone please suggest how to pass this controller variable value to the javascript function and display the truncated value on Visualforce page.




Srilakshmi B


hi srilakshmib87,

you can use the <apex:actionFunction> to send a parameter to javascript function which can communicate to the controller.


if you wanna use the variable from controller directly you can try using  it this way in the script tag


var temp=toFixed('{!varname}');




Hope it helps,




Hi Thank you for your idea.It will work.But I want to display the variable value in the apex:outputText how can I do that.


hierarchy in my VF page is  similar to following code but i cannot see the value on outputtext


<apex:form id="formid">

<table id"table">



<apex:outputText id="txtid"/>




var a={!variable}//from controller class





Can you please suggest where I am going wrong.




The id is not 'txtid' it will be something like 'j_id0:txtid' for every visualforce element it appends some text for the elements id.

go to view source to see the exact id names




hoep it helps,



See the description of $Component for how to get the correct DOM-id for a Visualforce componen:


Hi I clicked on View Source but I am not able to see the exact id .

With the id I have specified it is not working.




Don't use view source to try to figure out the if.   Use $Component to get it for you.  See the link I provided above.


Hi all,


i want to pass id which i displayed in <apex:outputtext> on vf page to my javascript variable.


i used

var cc=document.getElementById('{!$Component.fromMain.pb2.outid}');



my vf page code is

<apex:form id="fromMain">
<apex:pageBlock id="pb2">
    <apex:outputText id="outid" value="{!cid}" escape="false"/>




in alert i am getting  [object HTMLSpanElement]


so i am not getting id value.


so can anyone help me in getting id value as i am getting id values from controller.


public String getJSStart()
       return '<script>document.write(';
   public String getJSEnd()
       return ')</script>';


   var str="Hello world!";
                    <apex:outputText value="{!JSStart}str{!JSEnd}" escape="false" />