You need to sign in to do that
Don't have an account?

Geeting the value entered in inputfield in javascript.
Hi All,
I have standardcontroller and used controller extension.I have inputfield rejectionreason and I have a requirement in which user clicks on Reject button I should show alert message to the user to enter the rejection reason before rejecting record.
On the reject button I am calling the onclick event which calls javascript function.The vf code and controller code is as follows:
In the javascript variable I am not able to get the value that the user enetrs in the Rejection reason field.I think because onclick event is getting called before the action event and getter and setter methods are not executed.
Could you please let me know how this could be resolved and get the Rejection reason value what user enters in javascript variable.
VF Page code:
<apex:page standardController="object1__c" extensions="ApproveorReject" >
<apex:form>
function func()
{
var a='{!RejectionReason}';
if(a=='')
{
window.alert("Please enter the Rejection Reason before Rejecting the Invoice");
}
}
<apex:inputField value="{!object1__c.Rejection_Reason__c}" id="rejectionid"/><br></br>
<apex:commandButton value="Reject" action="{!reject}" onclick="func()"/>
</apex:form>
</apex:page>
Controller code:
public with sharing class ApproveorReject {
public String Rejectionreason;
public final object1__c jc;
public void setRejectionReason()
{
Rejectionreason=jc.Rejection_Reason__c;
}
public String getRejectionReason()
{
return Rejectionreason;
}
public ApproveorReject (ApexPages.StandardController controller)
{
this.jc = (object1__c)Controller.getRecord();
}
public PageReference reject()
{
//code
}
}
Hi,
instead of using var a='{!RejectionReason}';
use
var a= document.getElementById("{!$Component.form.fld}");
where form and fld are the id of form tag and inputfield tag, respectively.
then a.value gives you the value of that inputtext field
Hi,
You need to use parameter passing in visualforce. Refer following fields. You will get an idea to solve your issue.
http://salesforceworld.blogspot.com/2011/06/parameter-passing-using.html
http://salesforceworld.blogspot.com/2011/06/javascript-with-visualforce-pages.html
http://salesforceworld.blogspot.com/2011/06/summer-11-new-features-part-1.html
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Chamil's Blog
To access the Javascript value, you need to use the Javascript DOM, via the VisualForce component's id. To get the components id, you should use the '{!$Component..}' merge field (see here for further info).
In your current Javascript function, the value '{!RejectionReason}' will only contain the value from when the page was last loaded, not the new value.
In addition, if you want to prevent the '{!reject}' action being executed, the function assigned to the 'onclick' could return false.
Based on the VisualForce page you provided:
<apex:page standardController="object1__c" extensions="ApproveorReject" >
<apex:form>
<script type="text/javascript">
function func() {
var value = document.getElementById('{!$Component.rejectionid}').value;
if( value=='') {
window.alert("Please enter the Rejection Reason before Rejecting the Invoice");
return false; //return false if you don't want the '{!reject}' action to execute;
}
}
</script>
<apex:inputField value="{!object1__c.Rejection_Reason__c}" id="rejectionid"/><br></br>
<apex:commandButton value="Reject" action="{!reject}" onclick="return func()"/>
</apex:form>
</apex:page>
Hope this helps!