You need to sign in to do that
Don't have an account?
Reg: passing value from javascript function to apex controller
Hi,
Can any one help to this scenario.
Page Code:-
<apex:page controller="xyzcon" >
<script>
function addElement(value)
{
//alert(value);
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.type='Button';
<!-- If conditions that helps to add a control dynamically based on field types -->
if(value=='STRING')
{
newdiv.innerHTML = 'Name:<input type="+value+" Name="DVF__STRING" />';
}
if(value=='EMAIL')
{
newdiv.innerHTML = 'Email:<input type="+value+" Name="DVF__EMAIL" />';
}
if(value=='PHONE')
{
newdiv.innerHTML = 'Phone:<input type="+value+" Name="DVF__PHONE"/>';
}
if(value=='PICKLIST')
{
newdiv.innerHTML = 'Location:<select type="+value+" Name="DVF__PICKLIST"><option value="Hyderabad">Hyderabad<option value="Banglore">Banglore<option value="Chennai">Chennai';
}
if(value=='TEXTAREA')
{
newdiv.innerHTML = 'About You:<textarea rows="5" cols="20" Name="DVF__TEXTAREA"/>';
}
if(value=='BOOLEAN')
{
newdiv.innerHTML = 'CheckBox:<input type="Checkbox" Name="DVF__BOOLEAN"/>';
}
if(value=='DATE')
{
newdiv.innerHTML = 'Date:<input type="text" Name="DVF__DATE"/>';
}
ni.appendChild(newdiv);
}
<!-- function that helps to get the form elements -->
function getfieldvalues(oForm)
{
str = oForm.name;
for (i = 0; i < oForm.length; i++)
{
str += oForm.elements[i].name+ " -- "+ oForm.elements[i].value + "\n";
}
alert(str);
}
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!Data}" var="Fvalues">
<apex:column headervalue=" Field Names ">
<a href="javascript:;" onclick="addElement('{!Fvalues}')">{!Fvalues}</a>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<input type="hidden" value="0" id="theValue" />
<div id="myDiv"> </div>
<input type="button" value="Submit" onclick="getfieldvalues(this.form)"/>
</apex:form>
</apex:page>
Controller Code:-
public class xyzcon
{
List<Schema.DisplayType> fieldtypelst= new List<Schema.DisplayType>();
public List<Schema.DisplayType> getData()
{
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType systemObjectType = gd.get('E__C') ;
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap();
Set<String> fieldNames = M.keySet();
for(String fieldName : fieldNames)
{
Schema.SObjectField field = M.get(fieldName);
Schema.DescribeFieldResult fieldDesc = field.getDescribe();
if(fieldDesc.iscustom())
fieldtypelst.add(fieldDesc.getType());
}
return fieldtypelst;
}
}
How can i pass 'str' value from JavaScript method to apex Controller.
Can u please suggest me.
Thanks & Regards;
Ramya
You can use apex:inputHidden or apex:param to pass the variables to controller.
If a property is declared in controller as
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>
Give this a try.
with best,
All Answers
You can use apex:inputHidden or apex:param to pass the variables to controller.
If a property is declared in controller as
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>
Give this a try.
with best,
Hi,
I tried with this
Class:
public class CTest {
public String gettest2{get;set;}
public void test(){
System.debug('VARIABLE MYPROPERTY------'+ '******' + gettest2);
}
public CTest(ApexPages.StandardController controller) {
gettest2=null;
}
}
VF Page content:
<apex:page standardController="Account" extensions="CTest" showHeader="false">
<script>
function test2(){
String width='123';
document.getElementById('inpthddn2').value=width;
}
</script>
<apex:form >
<apex:commandButton action="{!test}" value="Try it" />
<apex:inputHidden value="{!gettest2}" id="inpthddn2" />
</apex:form>
{!gettest2}
</apex:page>
----------------------------------------------------------------------------------------------
Don't find any value of the variable width in apex variable gettest2.
please help me. as I tried to follow your Solution.
have u found solution???
below is the code :
<script type="text/javascript">
function chk(checkbox,id)
{var s;
if(checkbox.checked==true)
{
s=id;
myFun(s);
}
}
</script>
<apex:actionFunction name="myFun" action="{!myTest}" >
<apex:param name="a" value="" assignTo="{!myProperty}"/>
</apex:actionFunction>