lezbfranz
(new user )
Fri Feb 18 2005 06:57 PM
clear textbox

I am new to this forum and asp. I did read a earlier thread which helped me ---I want to clear a textbox when the user clicks on it--I am putting a default value in it but if they want to enter a value I want the textbox to clear when they click it. The solution I saw in this forum was:
<html>
<head>
<script language="javascript">
function cleartext()
{
document.form1.textbox.value = ""
}
</script>
</head>
<body>
<form name="form1">
<input type="text" value="Enter Keywords here..." onclick="cleartext()">
</form>
</body>
</html>

When I use this and run it --and then click on a textbox field I get a message at the bottom of the browser "error on page". Now this script is running in iis. I also tried changing the script to VBscript. Does anyone know why I am getting this error??

THANKS!


Nanook
(regular)
Fri Feb 18 2005 08:24 PM
Re: clear textbox

Don't know why you are getting the error but you can use this code instead:

<form name="form1">
<input type="text" value="Enter Keywords here..." onFocus="value=''">
</form>

To see an example of this see www.bains007.co.uk (my site; it's on the Google search box)


lezbfranz
(new user )
Fri Feb 18 2005 11:11 PM
Re: clear textbox

THANKS! That worked---not quite like I want but it will do! I really appreciate the help!!

SMILES!



Nanook
(regular)
Fri Feb 18 2005 11:11 PM
Re: clear textbox

No probs, what was it you wanted to do?

http://www.bains007.co.uk


slapmatt
(regular)
Mon Feb 21 2005 11:25 AM
Re: clear textbox

nanook - the problem with your code is that it clears the text box EVERY time the user clicks on it.

My code will only clear the text box if the text in the box is the same as your default text.

<html>
<head>
<title>
</title>
<script>
function clearText()
{
if (document.myForm.keyword.value == document.myForm.defaultText.value)
{
document.myForm.keyword.value = "" ;
}
}
</script>
</head>
<body align="center">
<form name="myForm">
<input type="text" name="keyword" value="Enter keyword here..." onClick="JavaScript: clearText()">
<input type="hidden" name="defaultText" value="Enter keyword here...">
</form>
</body>
</html>


Nanook
(regular)
Mon Feb 21 2005 03:53 PM
Re: clear textbox

Sorry, hadn't quite read the script correctly, don't know how to do what you want, search Google and there are lots of results

http://www.bains007.co.uk


Matt
(regular)
Wed Feb 23 2005 01:05 PM
Re: clear textbox

Tried that code, but it doesn't seem to like existing forms, with different names.




slapmatt
(regular)
Wed Feb 23 2005 02:20 PM
Re: clear textbox

Change the names then!


Matt
(regular)
Wed Feb 23 2005 02:40 PM
Re: clear textbox

I've changed the names in the bit in <head> section of code, but it still doesn't work. I can't change my forms' names to MyForm either, as there are two different forms on one page, with different names.


slapmatt
(regular)
Thu Feb 24 2005 08:32 AM
Re: clear textbox

OK, try this badboy.

<html>
<head>
<title>
</title>
<script>
function clearText(textElement, defaultElement)
{
if (textElement.value == defaultElement.value)
{
textElement.value = "" ;
}
}
</script>
</head>
<body align="center">
<form name="myForm">
<input type="text" name="keyword" value="Enter keyword here..." onClick="JavaScript: clearText(this, document.myForm.defaultText);">
<input type="hidden" name="defaultText" value="Enter keyword here...">
</form>
</body>
</html>

The only think you'll need to change is the document.myForm.defaultText so that it points to the hidden element in your form that holds the default text.

Therefore, if your form name is MattsForm and the hidden element in the form that holds the default text is called MattsDefaultText you'll need to change the line to:

document.MattsForm.MattsDefaultText


Contact Us | Privacy statement Main website
Hitwise Top 10 Award Winner - Jan-Mar 2005

About us | Contact us | Link to us | Terms & Conditions | Privacy Policy
© Copyright IPC Media Limited, All rights reserved