PDA

View Full Version : Clear a value from search box



jc_gmk
08-29-2007, 07:49 AM
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?

jc_gmk
08-29-2007, 07:56 AM
don't worry i've sorted it.

just did the following:



<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>

tech_support
08-29-2007, 10:45 AM
<script language="javascript">
Deprecated. Use: <script type="text/javascript">


document.form.search.value = "";
Form elements aren't properties of a document. Use: document.forms['formname'].elements['fieldname']


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:



<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>

jscheuer1
08-29-2007, 02:13 PM
There was an interesting approach to this developed in this thread:

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

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

http://www.dynamicdrive.com/forums/showpost.php?p=103841&postcount=12