I want to pass the java-script value to controller.I am using repeat on VF page.

VF page

    <apex:repeat value="{!listAdj}" var="Adj" id="hrs">

     <td><div class="requiredInput"><div class="requiredBlock"></div>
    <apex:inputField id="decimal" value="{!Adj.Hours_Decimal__c}" onchange="setHidden5(this);" >

     <apex:inputField id="hours" value="{!Adj.Hours__c}" />
<apex:inputField id="decimal" value="{!Adj.Hours_Decimal__c}" onchange="setHidden5(this);"/>


Javascript code

function setHidden5(element){
    //split the id of the inputField by ':' and then use the 5th index in the array to exact position of the inputField in the apex:repeat.
  var idArray = element.id.split(':');
    var hiddenRep1 =document.getElementById("thePage:form:hrs:hrs:"+idArray[4]+":decimal");
         var hiddenRep2 = document.getElementById("thePage:form:hrs:hrs:"+idArray[4]+":hours");

    if(element.value.length >=1){

      document.getElementById("thePage:form:hrs:hrs:"+idArray[4]+":hours").disabled = true;

I want to pass the hiddenRep2.value to controller.How to pass that. Hidden2.value is same as hidden1.value + some addition subtraction.i dont know how to pass that value. Can someone me sort out this.


2 Answers 2


There are multiple solutions for this question.

1. Use of hidden field

You can define an apex variable and associate it in a hidden input field on the visualforce page. Then set a javascript variable to this field an reference it in controller:

Apex class:

public class myClass {
    // Our test variable        
    public String myString {get; set;}

    public myClass(){
        myString = '';

    // Method for testing a hidden field functionality 
    public PageReference myMethod(){
        System.debug('myString: ' + myString);
        return null;

Visualforce page:

function setVar(param){

<!-- Hidden field to store a new value of the variable -->
<apex:inputHidden value="{!myString}" id="myHiddenField"/>

<!-- Action function for the rerendering -->
<apex:actionFunction name="passStringToController" action="{!myMethod}" rerender="myHiddenField"/>

<!-- A command button for sending a call to the function -->
<apex:commandButton value="Test me" onclick="setVar('new value'); return false;" />

2. Use of apex:param

// We don't need any javascript function anymore
// because we will access an actionFunction directly

<!-- A new value will be set to the apex:param -->
<apex:actionFunction name="passStringToController" action="{!myMethod}" rerender="myHiddenField">
    <apex:param name="p1" value="" assignTo="{!myString}" />

<!-- Here we can directly access the action function per name and assign a variable value -->
<apex:commandButton value="Test me" onclick="passStringToController('new value'); return false;" />
  • I am using repeat..can you please help me pass values when i am using repeat...??
    – miku
    Jan 21, 2014 at 10:52
  • @mastOr can you explain me this jquery code jQuery('[id$=myHiddenField]') how we can directly refer apex element id here, as it got modifed in browser.
    – Ankuli
    Sep 22, 2014 at 6:48
  • 1
    @Ankuli This jQuery code access an DOM element where ID ends with the given string (myHiddenField). Sep 22, 2014 at 7:24
  • @MichaelSobczak You are welcome! :) Oct 16, 2015 at 6:30

okay, this is a rather old question. But considering community (experience cloud) still only support vf page for login/reset password etc. I just found a new easier way to achieve it.

Instead of creating those action functions with params. Cookie is more straightforward, relatively speaking.

In your vf page javascript,

  var cookieString = "apex__YourCookieName=" +YourJSVar;
  document.cookie = cookieString;

You will need to append apex__ so apex controller can get it in the cookie class.

In your apex controller,

Cookie ck = ApexPages.currentPage().getCookies().get('YourCookieName');

Or just simply

String varValue = ApexPages.currentPage().getCookies().get('YourCookieName').getValue();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.