PDA

View Full Version : Javascript textfield



adityadubey
August 22nd, 2005, 02:57 AM
I already have a value in a textfield that says "email". How can I make the value empty on focus?

Sniper Jo
August 22nd, 2005, 10:37 AM
hello, i think it is document.FORMNAME.FEILDNAME.value =''

Ankou
August 22nd, 2005, 04:18 PM
Be careful how you use that. Clearing the value of a field on focus means that anytime someone puts focus onto that field it'll be "erased". So if someone fills in all their information and then accidently clicks on the email field it'll get erased. Usually not a huge deal but it can be frustrating to people who tab through the links/fields. I know I do that a lot when filling in forms.

You may want to consider checking to see what that value of that field is before you clear it. If it equals "email" then clear it. If it's not then leave it be.

adityadubey
August 23rd, 2005, 06:40 AM
thanks! i tried it but it's not working :(

Ankou
August 23rd, 2005, 02:38 PM
What does you code look like?

Here's a simple example.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Clear Email Example</title>
<script type="text/javascript">
function clear_email(){
var email = document.emailForm.emailField.value;
var expression = /^[ ]?email[ ]?$/i;
if(expression.test(email)){
document.emailForm.emailField.value = '';
}
}
</script>
</head>
<body>
<form name="emailForm">
<input type="text" name="emailField" value="email" onfocus="clear_email()" />
</form>
</body>
</html>


Obviously there's a lot missing from this form, but it's just to serve as a working example so you can see one method to do this.


If you don't care about the value getting erased every time focus is brought to the field you can do this:



<form name="emailForm">
<input type="text" name="emailField" value="email" onfocus="this.value=''" />
</form>