+ Start a Discussion
Sivakumari2iSivakumari2i 

How to clear input text fields

hi,

 

I have some input field in my visualforce page.

 

If i click a command button, the values entered must be cleared automatically.

 

I tried this using javascript but i am not able to achieve the end result.

 

Please help me.

 

Regards,

S.Sivakumar.

Best Answer chosen by Admin (Salesforce Developers) 
gautam_singhgautam_singh



Hi ,

 

I have made a lookup and a test field from Contact and tried to clear it with the link Clear .

<apex:page standardController="Contact" id="page">
    <apex:form id="frm">
        <apex:pageblock id="pblck">
            <apex:pageblockSection id="pbSectn" >
                <apex:pageblockSectionItem id="pbSitemLookup">
                    <apex:inputField id="accountLookup" value="{!Contact.AccountId}"/>
                </apex:pageblockSectionItem>
                
                <apex:pageblockSectionItem id="pbSitemLastName">
                    <apex:inputField id="contactLastname" value="{!Contact.LastName}"/>
                </apex:pageblockSectionItem>
                
                <apex:pageblockSectionItem id="pbSitemLink">
                    <b><a href="#" onClick="clearValue()" > Clear </a> </b> 
                </apex:pageblockSectionItem>
            </apex:pageblockSection>
        </apex:pageblock>
        
        <p> <apex:commandButton action="{!Save}" value="Save" id="cmdSave"/> </p>
    </apex:form>
    
    <script>
    function clearValue()
    {
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLookup:accountLookup}').value = '';
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLastName:contactLastname}').value = '';
  
    alert();
    return false;
    }
    
    </script>
    
    
</apex:page>




Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You .

All Answers

Bhawani SharmaBhawani Sharma
1. you can write a method in controller class to nullify these values. Like
public void nullify() {
field = null;
}
use this method on a command button like
<apex:commandButton action="{!nullify}" value="Reset" />

2. Use javascript to do this. Write a javascript method and call it on button's onclick
like
document.getelementbyId('Your field html Id').value = null;
gautam_singhgautam_singh



Hi ,

 

I have made a lookup and a test field from Contact and tried to clear it with the link Clear .

<apex:page standardController="Contact" id="page">
    <apex:form id="frm">
        <apex:pageblock id="pblck">
            <apex:pageblockSection id="pbSectn" >
                <apex:pageblockSectionItem id="pbSitemLookup">
                    <apex:inputField id="accountLookup" value="{!Contact.AccountId}"/>
                </apex:pageblockSectionItem>
                
                <apex:pageblockSectionItem id="pbSitemLastName">
                    <apex:inputField id="contactLastname" value="{!Contact.LastName}"/>
                </apex:pageblockSectionItem>
                
                <apex:pageblockSectionItem id="pbSitemLink">
                    <b><a href="#" onClick="clearValue()" > Clear </a> </b> 
                </apex:pageblockSectionItem>
            </apex:pageblockSection>
        </apex:pageblock>
        
        <p> <apex:commandButton action="{!Save}" value="Save" id="cmdSave"/> </p>
    </apex:form>
    
    <script>
    function clearValue()
    {
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLookup:accountLookup}').value = '';
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLastName:contactLastname}').value = '';
  
    alert();
    return false;
    }
    
    </script>
    
    
</apex:page>




Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You .

This was selected as the best answer
Sivakumari2iSivakumari2i

Thanks.

 

Its working for me.

 

So we need provide the correct path to achieve this.

 

Regards,

S.Sivakumar