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
nbknbk 

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&colon;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; }

  }

Best Answer chosen by Admin (Salesforce Developers) 
Devendra NataniDevendra Natani

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

Devendra NataniDevendra Natani

 

 

Controller class- 

 

public class formController {
public MyObject mObject{get;set;}

public formController (){
mObject = new MyObject ();
}
public class MyObject {

public String textData {get;set;}
public MyObject (){
textData = 'samplevariable';
}
}

}

 

VFP - 

 

<apex:page id="pageForm" controller="formController">
    <apex:form id="form">
        {!mObject.textdata}
         <apex:inputHidden value="{!mObject.textdata}" id="hidBusiness"/>
        <apex:commandButton id="btnSave" onclick="javascript&colon;if(!confirmation()) return false;" value="save"/>
    <script>
       function confirmation()
        {
          var hidBusiness= document.getElementById("{!$Component.hidBusiness}");
          alert('Hi'+hidBusiness);
        }
    </script>
    </apex:form>
</apex:page>

 

I have updated the controller class and vf page. In the vf page I have put the following code for testing purpose.

{!mObject.textdata}

You can run the vf page and you can see the value of textdata.

 

 

 

 

nbknbk

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.

 

 function confirmation()
        {
          var hidBusiness= document.getElementById("{!$Component.hidBusiness}");
          alert('Hi'+hidBusiness); //showing NULL --expecting SampleVariable
        }

Thanks,

Krishna

 

 

nbknbk

Devendra,

 

got the value in javascript by adding value statement.

 var hidBusiness= document.getElementById("{!$Component.hidBusiness}").value;


Thank you very much

Devendra NataniDevendra Natani

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.

 

This was selected as the best answer
Chandan3955Chandan3955

Thanks..   :smileyhappy: