Solved

JavaScript - clear default value of input form field

Posted on 2011-02-15
5
756 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

726 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