Results 1 to 4 of 4

Thread: Clear a value from search box

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Clear a value from search box

    I've created a search box and have set the value to 'Product Search...'

    What I want to be able to do is have that value cleared as soon as someone clicks in the box to enter their search.

    Is this possible? I'm assuming i'd need some javascript and the onClick function?

  2. #2
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    don't worry i've sorted it.

    just did the following:

    HTML Code:
    <script language="javascript"> 
    function cleartext() 
    	{ 
    	document.form.search.value = "";
    	} 
    </script>
    
    <form name="form" action="search.php" method="get">
    <input type="text" name="search" value="Product Search..." onClick="cleartext();" />
    <input type="submit" value="Search" />
    </form>

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Code:
    <script language="javascript">
    Deprecated. Use: <script type="text/javascript">
    Code:
    document.form.search.value = "";
    Form elements aren't properties of a document. Use: document.forms['formname'].elements['fieldname']
    Code:
    onClick="cleartext();"
    This means that every time he clicks the field, it'll clear. Now, we don't want that do we?

    Try this code:

    HTML Code:
    <script type="text/javascript">
    var isClicked = false
    function cleartext() {
    	if (!isClicked)	{
    		document.forms['form'].elements['search'].value=''
    		isClicked=true
    	}
    }
    </script>
    <form name="form" action="search.php" method="get">
    	<p>
          <label><input type="text" name="search" value="Product Search..."></label>
        </p>
        <p>
          <label><input type="submit" name="Submit" value="Search"></label>
        </p>
    </form>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There was an interesting approach to this developed in this thread:

    http://www.dynamicdrive.com/forums/s...ad.php?t=23313

    The most succinct working example of which was in this post:

    http://www.dynamicdrive.com/forums/s...1&postcount=12
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •