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
Jayesh Babu A VJayesh Babu A V 

get <apex:input> value on button click

I have created a datepicker inside the visualforce page and I want to get value of the <apex:input> into a variable, and access it on buton click. this is the code: 
<apex:page docType="html-5.0" showHeader="false" 
           controller="ShowResourcesClass" >

     <apex:form >
    		<apex:pageBlock >
        		<apex:pageBlockSection >
                             <apex:pageBlockSectionItem > 
                		    Date: <apex:input type="date" value="{!chosenDate}"/>
            		     </apex:pageBlockSectionItem>
        		</apex:pageBlockSection>
            </apex:pageBlock>
        </apex:form>
        
        <apex:form >
        	<apex:commandButton action="{!getDateFun}" value="Submit" id="theButton"/>
        </apex:form>

</apex:page>

And this is the apex class
public class ShowResourcesClass {
    public Date chosenDate {get;set;}

    public void getDateFun() {
        System.debug('DATE: ' + chosenDate);
     }

}
Here, when I click the buttton even  after choosing the date, the value of `chosenDate` is always null. 
AnudeepAnudeep (Salesforce Developers) 
Hi Jayesh, 

Can you verify if chosenDate displays any value when it is placed inside the constructor?
public class ShowResourcesClass {
    public Date chosenDate {get;set;}
    public ShowResourcesClass() {
         System.debug('DATE: ' + chosenDate); 
    }
}
Also, can you try using onclick on apex:input itself. This component supports HTML pass-through attributes using the "html-" prefix. Pass-through attributes are attached to the generated <input> tag so try passing the attributes as showing in the references below

Reference: 

https://developer.salesforce.com/docs/atlas.en-us.224.0.pages.meta/pages/pages_html_features_pass_through_attributes.htm

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_input.htm

If you find this answer helpful, please mark this as solved by selecing this as Best Answer so that it may help others in the community. Thank You!

Anudeep