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
balakrishna mandula 6balakrishna mandula 6 

Javascript

I am new to javascript. I want to learn JavaScript with simple example.

Below is the code which is not executing
<apex:page >

<apex:outputPanel >
   <apex:pageMessages id="demo"/>
</apex:outputPanel>

<script type = "text/javascript">
  function myfunction(){
    var l = 10;
    var b = 20;
    var ar = l * b;
    document.getElementById("demo").innerHtml = ar;
  }
</script>  
</apex:page>

The above code is not giving area.

Please help me out.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
You should use your javascript in VF page.there is no link with your java script and Visualforcepage.You didnot use the function in the VF tag
Andy BoettcherAndy Boettcher
Bala,

your document.getElementById line is incorrect - if you inspect the VF page in Chrome Developer Tools, you will see that VF components are rendered with system-generated Ids, even if you specify the Id in the apex:pageMessages component.  Just switch out "document.getElementById("demo").innerHtml = ar;" to:

document.getElementById("something:something:something:something:demo").innerHtml = ar;

Where the "somethings" are the system-generated part you will see when you inspect that element.

pbattissonpbattisson
As per Andy's answer you should use the correct component Id as these are dynamic in Visualforce. The correct way to do this is using the $Component visualforce formula. You can read more about it here (https://www.salesforce.com/us/developer/docs/pages/Content/pages_best_practices_accessing_id.htm) and a good example is shown here  (https://www.salesforce.com/us/developer/docs/pages/Content/pages_access.htm)but a simple update to Andy's code is:

document.getElementById("{!$Component.demo}").innerHtml = ar;