[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

JavaScript - clear default value of input form field

Hi,
is there a way of automatically clearing "Find Products" when a user click in the box??

Thanks..

<html>
<head>
</head>
<body>
<form name="search" method="POST" action="search.php">
                    <input name="Submit" type="submit" id="Submit" value="Go" />
                    <input name="keywords" type="integer" class="searchinputbox" id="keywords" value="Find Products" size="15">&nbsp;&nbsp;&nbsp;
</form>
</body>
</html>
0
sabecs
Asked:
sabecs
1 Solution
 
Lee SavidgeCommented:
Change this:

<input name="keywords" type="integer" class="searchinputbox" id="keywords" value="Find Products" size="15">&nbsp;&nbsp;&nbsp;

to

<input name="keywords" type="integer" class="searchinputbox" id="keywords" value="Find Products" size="15" onclick="javascript:this.value="";">&nbsp;&nbsp;&nbsp;

This doesn't cater for uses tabbing into the box though.

Lee

0
 
Gurvinder Pal SinghCommented:
<input name="keywords" type="integer" class="searchinputbox" id="keywords" value="Find Products" size="15" onclick="clearField(this)" onfocus="clearField(this)">

<script>
  function clearField(this)
  {
      if (this.value == "Find Products")
      {
          this.value = "";
      }
  }
</script>


or you can simply try placeholder

<input name="keywords" type="integer" class="searchinputbox" id="keywords" placeholder="Find Products" size="15" onclick="" onfocus="">
0
 
Lee SavidgeCommented:
0
 
Tomarse111Commented:
just going to add tiny change to gurvinders solution, the use of defaultValue is nicer than having to use the physical string to compare against.
<input name="keywords" type="integer" class="searchinputbox" id="keywords" value="Find Products" size="15" onclick="clearField(this)" onfocus="clearField(this)">
<script type="text/javascript">
  function clearField(x){	  
      if(x.value==x.defaultValue){x.value="";}
  }
</script>

Open in new window

0
 
sabecsAuthor Commented:
Thanks, its perfect...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now