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
srilakshmib87srilakshmib87 

Use of Javascript function in Visualforce page

Hi..,

 

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.

 

 

Thanks

Srilakshmi B

SidzSidz

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

<script>

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

</script>

 

 

Hope it helps,

Sidz

 

srilakshmib87srilakshmib87

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">

<tr>

<td>

<apex:outputText id="txtid"/>

</td>

</tr>

<script>

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

document.getElementById('formid.table.txtid').Value=a.toFixed(2);

</apex:form>

 

 

Can you please suggest where I am going wrong.

 

SidzSidz

hi,

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,

SIdz

aballardaballard

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

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_best_practices_accessing_id.htm?SearchType=Stem

srilakshmib87srilakshmib87

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.

 

 

aballardaballard

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.

sunil_kumarsunil_kumar

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}');

alert(cc);

 

my vf page code is

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

<apex:pageblock/>

<apex:form/>

 

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.

ThenmozhiThenmozhi

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

 

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