PDA

View Full Version : text appearing in a textbox



duke_1222
08-13-2006, 02:17 PM
Hi there - I have a simple query:

I am looking for some javascript code for an email text box:.

When the page loads the text in the text box will state
"Enter email address"

When the user clicks in the text box - the text will automatically disappear allowing the user to write his/her email address.

Thank you

:)

shachi
08-13-2006, 02:25 PM
It's really easy to make something like that.

here's the javascript:


<script type="text/javascript">
function erase(){
var target = document.someform.somename;
target.value = "";
}
</script>


here the form:


<form name="someform">
<input type="text" name="somename" onfocus="erase();" value="Enter your email address here">
</form>


there is also a more easier way:



<form name="someform">
<input type="text" name="somename" onfocus="this.value=''" value="Enter your email address here">
</form>

duke_1222
08-13-2006, 06:36 PM
Thank you - works great!

shachi
08-13-2006, 07:00 PM
You are welcome.:)

duke_1222
08-13-2006, 07:25 PM
One extra question:

Is it possible to only have that javacode run once (clearing the texdt box)

Otherwise if the user has entered the wrong address then he/she when going back to edit the code - the text box blanks everytime.

shachi
08-13-2006, 07:55 PM
I didn't get what you meant but I have a strong feeling that you meant something like this:



<html>
<head>
<script type="text/javascript">
function show(){
var target = document.someform.someinput;
if(target.value == ""){
target.value = "enter your email";
}
}
</script>
</head>
<body>
<form name="someform">
<input type="text" onfocus="this.value=''" onblur="show();" name="someinput" value="enter your email">
</form>
</body>
</html>



And it's javascript not java, java and javascript are *NOT* the same.

Snorkle?
08-13-2006, 08:26 PM
Or This Might Work As Long As You Have The Rest Done..

<body>
<form name="someform">
<input type="text" onFocus="this.value=' ' " onblur="this.value='Enter Your Email' name="someinput"
</form>
</body>

Sorry If This Doesnt Help You

mwinter
08-13-2006, 08:42 PM
What the OP is really looking for is something like:



function clearDefaultValue(element) {
if (element.value == element.defaultValue) element.value = '';
}




<input type="text" name="e-mail" value="Enter e-mail address"
onfocus="clearDefaultValue(this);">

That is, only clear the value of the element if it equals the default entry.

Note that this isn't typically a good idea. It should be obvious through other means - a label, more often than not - what should be used as the value of a control. Fiddling like this is unnecessary, and might even be confusing to some.

Mike