You need to sign in to do that
Don't have an account?

How to call class variable in Javascript
Hi All,
I need to use controller class variable in javascript onClick event. For this I used below code.
I am getting NULL value instead of "samplevariable" which hardcoded in class variable while click Javascript function and m
VFP:
<apex:page id="pageForm" controller="formController">
<apex:form id="form">
<apex:inputHidden value="{!myObject.textdata}" id="hidBusiness"/>
<apex:commandButton id="btnSave" onclick="javascript:if(!confirmation()) return false;" value="save"/>
<script>
function confirmation()
{
var hidBusiness= document.getElementById("{!$Component.hidBusiness}");
alert('Hi'+hidBusiness);
}
</script>
</apex:form>
</apex:page>
Controller class:
public class formController {
public class MyObject {
public String textdata = 'samplevariable';
public String getTextData() { return textdata; }
public void setTextData(String data) { textdata = data; }
}
Hi,
You can simply use like that -
var x = '{!mObject.textdata}';
you can check the value by using alert statement.
alert(x);
Please let me know if there is any issue.
All Answers
Controller class-
VFP -
I have updated the controller class and vf page. In the vf page I have put the following code for testing purpose.
You can run the vf page and you can see the value of textdata.
Hi Devendra,
I'm getting sample variable in VFP, but I need to comapare the variable in Javascript while click on Save button.
Is any way to call the variable in JavaScript.
Thanks,
Krishna
Devendra,
got the value in javascript by adding value statement.
var hidBusiness= document.getElementById("{!$Component.hidBusiness}").value;
Thank you very much
Hi,
You can simply use like that -
var x = '{!mObject.textdata}';
you can check the value by using alert statement.
alert(x);
Please let me know if there is any issue.
Thanks.. :smileyhappy: