/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Search term to find function to clear textbox unless exits with blank value?

I guess the title say it all:

Search term to find function to clear textbox unless exits with blank value?

I’m trying to find a function that clears a textbox of the default entry
and allows user to put in information. Would keep textbox value
unless exits with a blank value, in which case the default prompt is
replaced into the textbox.

I’ve tried searching with:
‘erase text on entry’
‘blank prompt textbox’
and several other combinations. 😮

I’ve tried the following, but it’s not working and I’m not getting any errors in FF? ?

[code]
<html>
<head>
<title>Clear Textbox Prompt</title>
<script type=”text/javascript”>
// From: http://www.webdeveloper.com/forum/showthread.php?p=1044548#post1044548

function clsIn(info,Msg) {
promptMsg = Msg;
if (info.name.value == Msg) { info.name.value = “”; }
}
var promptMsg = ”;
function clsOut(info) {
if (info.name.value == ”) { info.name.value = promptMsg; }
}

</script>
</head>
<body>
<form name=’myForm’ onsubmit=”return false”>

<input type=”text” value=”Last name” onfocus=”clsIn(this,’Last name’)” onblur=”clsOut(this)” name=’lname’ />
<input type=”text” value=”First name” onfocus=”clsIn(this,’First name’)” onblur=”clsOut(this)” name=’fname’ /><br>
<input type=”text” value=”Address” onfocus=”clsIn(this,’Address’);” onblur=”clsOut(this)” name=’addr’ /><br>
<input type=”text” value=”City” onfocus=”clsIn(this,’City’);” onblur=”clsOut(this)” name=’city’ />
<input type=”text” value=”State” onfocus=”clsIn(this,’State’);” onblur=”clsOut(this)” name=’state’ /><br>
<input type=”text” value=”Zip” onfocus=”clsIn(thisIn,’Zip’);” onblur=”clsOut(this)” name=’zipcode’ />

</form>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameOct 29.2009 — The thing you want is, form input's of the following types: text, radio, checkbox, password and file type input's have already got a DOM property named: defaultValue which you can use (if memory serves, defaultValue remains constant, it is what the input's value was when it was created, even if you change .value via script):
[code=html]<html>
<head>
<title>Clear Textbox Prompt</title>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?p=1044548#post1044548
function clsIn(info) {
if (info.value == info.defaultValue) {
info.value = "";
}
}

function clsOut(info) {
if (info.value == '') {
info.value = info.defaultValue;
}
}
</script>
</head>
<body>
<form name='myForm' onsubmit="return false">
<input type="text" value="Last name" onfocus="clsIn(this)" onblur="clsOut(this)" name='lname' />
<input type="text" value="First name" onfocus="clsIn(this)" onblur="clsOut(this)" name='fname' /><br>
<input type="text" value="Address" onfocus="clsIn(this);" onblur="clsOut(this)" name='addr' /><br>
<input type="text" value="City" onfocus="clsIn(this);" onblur="clsOut(this)" name='city' />
<input type="text" value="State" onfocus="clsIn(this);" onblur="clsOut(this)" name='state' /><br>
<input type="text" value="Zip" onfocus="clsIn(this);" onblur="clsOut(this)" name='zipcode' />
</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorOct 29.2009 — Well 'astupidname', you've managed to save my butt one more time.

Works well and I learned something I did not know about a 'defaultValue' assignment!

Appreciate the time.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...