+ Start a Discussion
shinerajbshinerajb 

Urgent : javascript variable to apex variable

My apex code is given i want to display the value of include2 on button click it is not working.
with regards
Shine
  ------------Apex code---------
public string timestampValue{get;set;}
public new2(){
timeStampValue = 'm';
}
public void sendTimestamp()
{
    timestampValue='timestampValue--' + timeStampValue;
//system.debug('timestampValue' + timeStampValue);
}

}
------------Apex code---------
-------------javascript code---------
var include2='';
function showIndustriesId(theId,obj)
{
    if(obj.checked)
    {
        include2=include2.concat((','+theId));
        alert(include2);
    }
    else
    {
        include2=include2.replace((','+theId),'');
        alert(include2);
    }
}
function sendTimeStamp()
{
    var timeStamp=include2;
          sendTimeStamp(timeStamp);
}

-------------javascript code---------
-------------visual force---------
<apex:form >
  <apex:commandButton value="pass" onclick="sendTimeStamp()"/>
          <apex:actionFunction name="sendTimeStamp" action="{!sendTimeStamp}" >
    <apex:param name="x" value="" assignTo="{!timeStampValue}" />
</apex:actionFunction>
    </apex:form>
-------------visual force---------

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

U can do like this

 

<apex:inputHidden value="{!YourObject.field}" id="theField1" />
       <script> var theField1 = document.getElementById('{!$Component.theField1}'); </script>
       <apex:commandButton onclick="theField.value=include2;" action="{!save}" value="save" />  

 

Regards,

Rajesh.       
     

AditiSFDCAditiSFDC
Hi,

I have tried this code block and using onclick="sendTimeStamp(); return false;"/> will solves your problem.
shinerajbshinerajb

Hai Aditi,

 


I tried this,but its not coming

shinerajbshinerajb

Hai,

 

We have values in javascript ,I want to get those values in an apex variable

Alex.AcostaAlex.Acosta

Another solution is to use the <apex:Param /> Tag.

 

You can find more about this tag by going to http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm


But here's a snipet of how it should look like using VisualForce's Tag Library to assign value to your apex variable in your controller. 

<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>