+ Start a Discussion
Newbie_edmNewbie_edm 

JavaScript Variable Value to Apex Method always returning 'null'

Guys,

I am trying to pass Javascript Variable value to apex method, it always returns null in apex method when i try to see the value using system.debug,  I did try the following solution from net but still not working, any thoughts?

thanks,

--------------------------------------------------------------------------------------------------------------------------------------------

public class myController {
  public string myProperty {get; set;}
 

  public void myTest() {
    System.debug('VARIABLE MYPROPERTY------' + myProperty);

  }

}
 
And on the visualforce you can pass javascript variable as -
 
Using InputHidden
<apex:inputHidden value="{!myProperty}" id="inptHdn"/>
 
<script>
  function passVariable() {
    String str = 'my test value';
    document.getElementById('<ID OF INPUT HIDDEN>').value = str;
  }
</script>
 
Using action function
 
<apex:actionFunction name="myFun" action="{!myTest}">
  <apex:param name="a" value="" assignTo="{!myProperty}"/>
</apex:actionFunction>
 
and you can call this action function from javascript method as
 
<script>
  function passVariable() {
    String str = 'my test value';
    myFun(str);
  }
</script>

-----------------------------------------------------------------------------------
Nayana KNayana K
In actionfunction add rerender="hiddenBlock"  and check once.
ManojjenaManojjena
HI Venkat,
Check on ething in the button click where you are executing the action function ,in that button code you need to add reRender attribute so that you will get the value in controller .
Let us know if it helps !!!
Thanks
Manoj
Newbie_edmNewbie_edm
Its still not working. Can you guys send me some sample code? 
sslodhi87sslodhi87

Hi Venkat,

Please check if this works for you 

First Way: Using action function

<apex:actionFunction name="myFun" action="{!myTest}" rerender="dummy">
                <apex:param name="a" value="" assignTo="{!myProperty}"/>
</apex:actionFunction>
 
 
<script>
  function passVariable() 
{
    String str = 'my test value';
    myFun(str);
  }
</script>


Second Way: Using InputHidden

 

Using InputHidden
<apex:inputHidden value="{!myProperty}" id="inptHdn"/>
 
<script>
function passVariable() 
{
    String str = 'my test value';
    document.getElementById('{!$Component.inptHdn}').value = str;
}
</script>
 

If you still facing issue then check if you have used any inputfield component which is required and you are not filling any value.

Better you post full code of your page.. then it will easy to understand.

Thanks
 

☯ BonY ☯☯ BonY ☯
Hi Venkat,

Try this
 
<apex:page controller="methodCall" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
               <apex:commandButton value='click' styleClass='saveButton' />
				<apex:outputText value="{!a}" label="Selected value" id='view' />
            </apex:pageBlockSection>
            <apex:actionFunction name="withparmeter" action="{!callmethod}" rerender="view">       
                <apex:param name="var1" value=""  assignTo="{!a}" />
            </apex:actionFunction> 
        </apex:pageBlock>
    </apex:form>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.saveButton').on('click', function(){
                var a ='test';
                withparmeter(a);
                return false;
            });
        });
    </script>
</apex:page>

Apex Cls
 
public class methodCall 
{
    public string a{get;set;}  
    public void callmethod()
    {
       system.debug('---------------'+a);
    }
  
}

 
Bharta ChandBharta Chand
You need to give your form a Id first otherwise if there is no Id of <apex: form> there will be auto-generated for Id in the element id like.

<input id="j_id0:j_id2:orgId" type="hidden" name="j_id0:j_id2:orgId" value="00ABCDXYZMNOIJK" />

where j_id2 is auto-generated for form element. 

<apex:form id="apForm">
        <apex:inputHidden value="{!orgId}" id="orgId"/>
</apex:form>    

and then in javascript access this element like below:

var  snapInName = document.getElementById('{!$Component.apForm.orgId}').value;

or,

document.getElementById('{!$Component.apForm.orgId}').value = 'xyz';