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
DipakDipak 

How to store javascript variable value in VFpage controller variable

Hi,

 

Can i acess java script varible value in a VF page Controller Apex class.

 

Suppose in my VF page , a java script variable   " var a='some thing'" . Now i want to store this variable in a Apex Controller class varible...

 

Is it possible...?

 

Best Answer chosen by Admin (Salesforce Developers) 
cwall_sfdccwall_sfdc

Note that Visualforce Javascript remoting is stateless. Remoted methods must be static and remoting does not integrate w/ viewstate.

 

For passing Javascript values to your controller, see apex:actionFunction:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm#kanchor858

 

For example:

...
    <apex:form>
        <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
            <apex:param name="firstParam" assignTo="{!state}" value="" />
        </apex:actionFunction>
        <input type="button" value="Click Me" onclick="clickMe()"/>
    </apex:form>

    <script>
        function clickMe() {
            methodOneInJavascript('INVOKED!');
        }
    </script>
...

 

 

All Answers

kiranmutturukiranmutturu

yes you can use java script remoting concept to send the value from page to controller or you can get the id of the component in the script and assing the value to that like

 

for ex:

 

if you have the component <apex:inputhidden value="{!hdnvalue}" id="hdnval"/>

then

<script>

var a = 'some thing';

document.getElementById.hdvbal.value = a;

</script> 

cwall_sfdccwall_sfdc

Note that Visualforce Javascript remoting is stateless. Remoted methods must be static and remoting does not integrate w/ viewstate.

 

For passing Javascript values to your controller, see apex:actionFunction:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm#kanchor858

 

For example:

...
    <apex:form>
        <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
            <apex:param name="firstParam" assignTo="{!state}" value="" />
        </apex:actionFunction>
        <input type="button" value="Click Me" onclick="clickMe()"/>
    </apex:form>

    <script>
        function clickMe() {
            methodOneInJavascript('INVOKED!');
        }
    </script>
...

 

 

This was selected as the best answer
DipakDipak

Thanks a lot for your suggestion

ziaullahkhaksarziaullahkhaksar

please help ...

how to pass javascript variable in apex code....

Thanks alot in Advance

 

DipakDipak

Pass the required value as parameter through javascript function ...

In below example it is "function MyMethod(id)"..From this method call a function (here it is "CallApexMethod(id)")Then program controller finds the "action function " having name "CallApexMethod(<argument>)"


<script LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
function MyMethod(id) {
     
      
       CallApexMethod(id);
      
  }
 
</script>

<apex:form id="PageForm">
    <apex:actionFunction name="CallApexMethod" action="{!process}" oncomplete="">
            <apex:param name="ID" value="" />
           
    </apex:actionFunction>
</apex:form>

__________________________

Get the value of param "ID" in Apex controller like

String Id=System.currentPageReference().getParameters().get('ID');

 

ziaullahkhaksarziaullahkhaksar

Thank YOu So Much ....

It was really very HelpFul

 

ziaullahkhaksarziaullahkhaksar

Please help ... I am very beginner to Visual Force ...

How to sent an e-mail through apex scheduler ?? I mean to sent an e-mail at fix time.