0

im using a simple text input form. and the "value" is a default.. such as saying "title" in the title text feild to let users know this is where you enter your title. How can i make this default text disappear when a user clicks in text box to make an entry w/o having to backspace the text out? I see this effect all over the net. any ideas?

3
Contributors
6
Replies
9
Views
7 Years
Discussion Span
Last Post by fuston05
0
<script type="text/javascript">
function setValue(field)
{
	if(''!=field.defaultValue)
	{
		if(field.value==field.defaultValue)
		{
			field.value='';
		}
		else if(''==field.value)
		{
			field.value=field.defaultValue;
		}
	}
}
</script>
<input type="text" value="Enter Title" onfocus="setValue(this)" onblur="setValue(this)"/>

Edited by hielo: n/a

0

that worked great, thank you very much.. However, I also have a "textarea", and it doesn't work for that. any ideas? Thanks so much for your help

0

for textarea, try innerHTML instead of value. You may have to work with the function a bit to get what you need, but the idea is to change the innerHTML content of the text area as it does not use the value field.

0

I tried with a textarea and it worked fine for me:

<script type="text/javascript">
function setValue(field)
{
	if(''!=field.defaultValue)
	{
		if(field.value==field.defaultValue)
		{
			field.value='';
		}
		else if(''==field.value)
		{
			field.value=field.defaultValue;
		}
	}
}
</script>
<input type="text" value="Enter Title" onfocus="setValue(this)" onblur="setValue(this)"/>
<textarea  onfocus="setValue(this)" onblur="setValue(this)">hi</textarea>

Not sure if you have some other event handler attached to your textarea.

0

in you function call you used "this" as feild. I thought at first i should replace that with the text i wanted to appear. but it doesn't work unless i too use "this".. very odd. what is "this"?? haha sorry if this is a dumb Q but I am a total noob here. I know basic php and just starting into js.

0

Thank you works perfectly! :)
and no other handlers on my form

Edited by fuston05: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.