I am setting a value of hidden field in javascript which is fired on button click but when controller function is called hidden field value is not passed along as I expected.

Visual Force Page:

<script type="text/javascript">         
    function getName() {
        var fileInput = $('#idFileUpload');
        var fileName = fileInput.get(0).files[0].name;
        if ($('#hdnDocName').val() != '') {
            return true;
            return false;
<apex:form id="upload-form">
    <apex:pageMessages />
    <apex:outputPanel rendered="{!accountID != ''}">
        <input id="hdnDocName" value="{!filename}" type="hidden" />
        Please select image: 
        <input id="idFileUpload" data-max-size="2097152" type="file" name="uploadfile" value="{!doc.body}" accept="image/x-png, image/gif, image/jpeg, image/jpg" />
        <apex:commandButton value="Upload" onClick="if (getName() === false) { return false; }" action="{!SaveAttachment}"/>    
    <apex:outputPanel rendered="{!accountID == ''}">
        No valid account selected


public String filename { get; set; }
public Pagereference SaveAttachment()
    return null;

debug statement returns null. How do I pass values?

2 Answers 2


You are binding the filename field into the page when it is generated but you are not taking advantage of Visualforce's automatic binding of values back into the controller. To do so in this case you will need to use apex:inputHidden:

<apex:inputHidden id="hdnDocName" value="{!filename}"/>

As the id value gets prefixed in the generated HTML you can use jQuery's ends with selector syntax to match the element:

  • 2
    You took the words right out of my keyboard!
    – sfdcfox
    Mar 17, 2016 at 20:55
  • @sfdcfox Was reading some Angular 2 docs today so 2 way binding ("banana in a box" syntax now) is on my mind.
    – Keith C
    Mar 17, 2016 at 20:58

Or you can do something like this

define an action Function that can take a param

 <apex:actionFunction action="{!SaveAttachment}"  name="SaveAttachment">
       <apex:param name="name" value=""/>

Then call it like this

 <apex:commandButton value="Upload" action="{!SaveAttachment}" >
      <apex:param name="name" value="{!inputValue}" assignTo="{!paramValue}"/>

or you can use an html button and call the action function like this

 <button type="button" onClick="SaveAttachment(passYourParamHere)"  />

In your controller you need to change your method to something like this

public Pagereference SaveAttachment()
    string filename = Apexpages.currentPage().getParameters().get('name');
    //make sure to escape the string just in case ;)
    return null;
  • I don't think the ApexPages parameters are related to the apex:param; think you will need to get the value from the paramValue controller field.
    – Keith C
    Mar 17, 2016 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .