Solved

JavaScript - clear default value of input form field

Posted on 2011-02-15
5
752 Views
Last Modified: 2012-05-11
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
Comment
Question by:sabecs
5 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 34895684
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34895689
<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
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 34895691
0
 
LVL 15

Accepted Solution

by:
Tomarse111 earned 500 total points
ID: 34895946
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
 

Author Closing Comment

by:sabecs
ID: 34896153
Thanks, its perfect...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now