+ Start a Discussion
AsitM9AsitM9 

java script variable's value to stored in apex variable

Controller:

public class A
{
public string  s{get;set;};
	public A()
	{
		A= //some how i would like to store the value of s(java script varicble in this variable)
	}

}


VF page:

<apex:page controller="A">
	<script>
		var s;
		s='Value to be passed to apex class';
	</script>
</apex:page>

 http://boards.developerforce.com/t5/Visualforce-Development/How-to-store-javascript-variable-value-in-VFpage-controller/m-p/347637#M41881

 

given approach is not working for me.

plz suggest.

 

Best Answer chosen by Admin (Salesforce Developers) 
ryanjuptonryanjupton

You'll have to modify your class a bit:

public class A{
    public String s {set; get;}
    
    public PageReference setVariable(){
        return null;
    }
}

 Then you can add the following VF to pass the variable (I've included an output section so you can get it working, remove this in your code:

<apex:page controller="A" >
    <apex:form >
    	<apex:actionFunction action="{! setVariable}" name="assignVariable" rerender="showValue">
            <apex:param name="myParam" assignTo="{!s}" value=""/>
    	</apex:actionFunction>
    </apex:form>
    
    <!-- test the call -->
    <apex:outputPanel onClick="assignVariable('My Value')" styleClass="btn">
    	Click Me 
    </apex:outputPanel>
    <!-- show result of call -->
    <apex:outputText id="showValue" value="{!s}"/>
</apex:page>

 

All Answers

hitesh90hitesh90

Hi Asit,

 

You can't get javascript value in constructor but you can get it any other function of apex class.

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

Avidev9Avidev9

THe solution given in the link WORKS!

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

 

But the catch is you wont be able to access the variable via constructor. You can access variable from a method , once the JS method is called i.e clickMe();

 

You can automatically call the js by calling the same using js or by using document.ready

 

 

 

 

ryanjuptonryanjupton

You'll have to modify your class a bit:

public class A{
    public String s {set; get;}
    
    public PageReference setVariable(){
        return null;
    }
}

 Then you can add the following VF to pass the variable (I've included an output section so you can get it working, remove this in your code:

<apex:page controller="A" >
    <apex:form >
    	<apex:actionFunction action="{! setVariable}" name="assignVariable" rerender="showValue">
            <apex:param name="myParam" assignTo="{!s}" value=""/>
    	</apex:actionFunction>
    </apex:form>
    
    <!-- test the call -->
    <apex:outputPanel onClick="assignVariable('My Value')" styleClass="btn">
    	Click Me 
    </apex:outputPanel>
    <!-- show result of call -->
    <apex:outputText id="showValue" value="{!s}"/>
</apex:page>

 

This was selected as the best answer