You need to sign in to do that
Don't have an account?
Newbie_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>
-----------------------------------------------------------------------------------
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>
-----------------------------------------------------------------------------------
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
Hi Venkat,
Please check if this works for you
First Way: Using action function
Second Way: Using InputHidden
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
Try this
Apex Cls
<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';