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
r1985r1985 

Getting the value of output field in javascript

Hi,

 

Kindly let me know how to get the value of outputField in javascript.

 

Thanks in advance,

MVP

vishal@forcevishal@force

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.

 

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

 

r1985r1985

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

vishal@forcevishal@force

:-O 

 

It works when I tested, can you remove all the comments and then try again, am sure it's caused because of that. 

LakshmanLakshman

Simply do it as below:

 

// The VF Page

<apex:page controller="OPValueInJs">
	<apex:form >
		<apex:outputField value="{!testAccount.Name}" id="op1"/>
		
		<script>
			alert("{!testAccount.Name}");
		</script>
	</apex:form>
</apex:page>

 

Regards,

Lakshman