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
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Default value in apex:inputText

Hi,

 

How to populate a default value in <apex:inputText> .So that when user enters the text box ,the value should disapper.

(like <input type="text" placeholder="Search "  in html />).

 

Can anyone please suggest me on how to implement this in salesforce apex.

 

Thanks in advance.

Navatar_DbSupNavatar_DbSup

Hi, 

 

You can try below code

 

--------------VF page --------------

<apex:page controller="inputtextprpolulate" id="page" >
<script>
function chekvalue(inputtextvalu)
{

    if(inputtextvalu.value=='hello')
    {
        document.getElementById('page:frm:iputtext').value='';
        
    }
   
    
}

function chkaftercahge(inputtextvalu)
{
     if(inputtextvalu.value.length<=0)
    {
        document.getElementById('page:frm:iputtext').value='hello';
    }
}
</script>
  <apex:form id="frm" >
  
  <apex:inputtext id="iputtext" value="{!s}" onclick="chekvalue(this)" onchange="chkaftercahge(this)" />
  
  </apex:form>
</apex:page>


------------- Apex Controller -----------------


public class inputtextprpolulate
 {
public string s{get;set;}
public inputtextprpolulate()
{
    s='hello';
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ICSteveICSteve

Nice solution Ankit, however I would recommend using onfocus and onblur instead of onclick and onchange, respectively. Otherwise if you click in and click away without typing anything, it will stay blank. Focus and blur will resolve this though.