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
RamyaKrishnaRamyaKrishna 

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&colon;;" 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

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.

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

Prafull G.Prafull G.

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,

This was selected as the best answer
DebajyotiDebajyoti

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.

 

 

Sfdc11Sfdc11

have u found solution???

Sha 8Sha 8
i am getting null value in 'myProperty

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>