1

after clicking clear button i want to clear the input text value.Now it's clearing fine.But the value remains same in the controller.It is not reflected in the apex. how to do this ?

vf: <apex:outputPanel id="showblock" style="display:none;"> <div id="clearFilterDiv"> <apex:pageblockSection columns="3"> <apex:repeat value="{!filterArray}" var="a"> <apex:pageBlockSectionItem > <apex:outputLabel value="{!a.label}"/> <apex:inputText value="{!a.SearchValue}" id="searchTextId"/> </apex:pageBlockSectionItem> </apex:repeat> <apex:pageblockSectionItem > <apex:outputPanel id="outptText"> <a href="#" onClick="clearValue()" id="clearValue" style="display:none;"> Clear Filter </a> </apex:outputPanel> <apex:commandButton value="Apply Filter" onclick="filterSearch();return false;"/> </apex:pageblockSectionItem> </apex:pageblockSection> </div> </apex:outputPanel> script function clearValue() { $("[id$=searchTextId]").val(''); } 
1
  • Why can't you use a simple pagecommandbutton, on click of that call a method in apex which empties the string. Like clearmethod(){searchstring = '';} Commented May 3, 2017 at 11:44

2 Answers 2

0

in your javascript function you are clearing the value from the page , from clear the value from class just add action to the button and in that action clear the values from the class also.

in clearvalue function you can clear the variable value from class itself.

6
  • Actually it is commandlink and also i have tried that too by using action function I am calling a method in controller and setting the input text value as null . Still the result is same Commented May 3, 2017 at 10:54
  • the after clearing the value from javascript you can call <apex:actionfunction>,and from action function you can call call method to clear the values. Commented May 3, 2017 at 11:01
  • thats the thing i tried. Here is my code : public void actionFunMethod() { filterArray=null; } <a href="#" onClick="clearValue()" id="clearValue" style="display:none;"> Clear Filter <apex:actionFunction name="caall" action="{!actionFunMethod}"/> </a> Commented May 3, 2017 at 11:02
  • have you debug actionFunMethod method is it calling from Page?? Commented May 3, 2017 at 11:18
  • thans Avijit. It worked as exactly you suggested !!! Commented May 3, 2017 at 12:36
1

Use the below code in place of yours :

function clearValue() { document.getElementById('myPageId:mnFrmId:mnPgBlkId:myEmlId').value = ''; } 

PS: the 'myPageId:mnFrmId:mnPgBlkId:myEmlId' is the id of the input text box. So please provide your id of the input text box which you want to clear.

Please refer the below VF page as an example for this :

<apex:page controller="MyClearTextBoxClass" id="myPageId"> <Script> var myValidate = function(){} myValidate = function(){ var getEmailVal = document.getElementById("myPageId:mnFrmId:mnPgBlkId:myEmlId").value; CallControlMeth(); } function clearValue() { document.getElementById('myPageId:mnFrmId:mnPgBlkId:myEmlId').value = ''; } </script> <apex:form id="mnFrmId"> <apex:actionFunction action="{!checkEmail}" name="CallControlMeth"/> <apex:pageBlock id="mnPgBlkId"> <table id="TblId"> <tr id="1stTrId"> <td> <apex:outputText > Email Address: </apex:outputText> </td> <td id="inpuTMailID"> <apex:inputText id="myEmlId" value="{!email}"/> </td> </tr> <tr id="2ndTrId"> <td> <apex:commandButton value="Click me!" reRender="mnFrmId" onclick="myValidate()"/> </td> <td> <apex:outputPanel id="outptText"> <a href="#" onClick="clearValue()" id="clearValue" style="display:block;"> Clear Filter </a> </apex:outputPanel> </td> </tr> </table> </apex:pageBlock> </apex:form> </apex:page> 

Controller :

public with sharing class MyClearTextBoxClass { public String email{get;set;} //contructor public MyClearTextBoxClass (){ } public PageReference checkEmail() { //Do some task System.debug('@@@@ email:'+email); PageReference myPage; myPage = new PageReference('/006/o'); return myPage; } } 
3
  • 1
    You should never hardcode these "j_id0:mnFrmId:mnPgBlkId:myEmlId" ids! Commented May 3, 2017 at 10:55
  • @partha, the values is getting cleared in client side , but not in server side (controller). That's what i am facing difficulty Commented May 3, 2017 at 11:13
  • @ram135868 , When you will set the value from your script to blank( as show in the above example), and call your controller method after that , it will automatically reset your value to blank. Please try the above example and check your debug. Once it is reset and then the controller method is called( let say we are saving it) it will flash out the value . Commented May 3, 2017 at 11:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.