I am posting a small example of a vf page and it's controller to help you. Let me know if you need anything else.
// The Controller
public with sharing class OPValueInJs
{
// a public account to display it's name on the vf page
public Account testAccount{get;set;}
public OPValueInJs()
{
// querying only an account for testing purpose
testAccount = [Select Name From Account limit 1];
}
}
// The VF Page
<apex:page controller="OPValueInJs">
<apex:form >
<apex:outputField value="{!testAccount.Name}" id="op1"/>
<script>
var opValue = ''; // this variable will store the value in the output Field
var id = '{!$Component.op1}'; // document id of the output field
opValue = document.getElementById(id).innerHTML; // getting the value in the output field
alert(opValue);
</script>
</apex:form>
</apex:page>
Hi,
I am posting a small example of a vf page and it's controller to help you. Let me know if you need anything else.
Its not working. The first two steps worked. But thebelow step is not working. And nothing is displayed in alert statement.
opValue = document.getElementById(id).innerHTML; // getting the value in the output field alert(opValue);
Thanks,
MVP
:-O
It works when I tested, can you remove all the comments and then try again, am sure it's caused because of that.
Simply do it as below:
Regards,
Lakshman