|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Zealot
![]() ![]() Join Date: Aug 2008
Posts: 168
|
Deleting initial value text from a form text field on click.
Hello everyone,
I have a simple text field in a form tag. I have initial value text. Basically the initial Value tells the user what the field is for. Eg: Password. Now when the user click in the field, the initial val text is deleted by an on click event. Code:
<input name="userName" type="text" id="uNameField" onclick="this.value='';" value="Enter username" maxlength="19" /> Is there a way to make the initial value delete on the first click in the text field but then whatever the user typed remains until the form is either submitted or the browser is refreshed or clear? I'm lost here. Any ideas will be appreciated. IC |
|
|
|
|
|
#2 |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,676
|
You might want to do this the other way around.
Have no default value, and if the user has Javascript enabled, then have the script add in the value "Set your username" just once. onblur, when the focus is set on the input, and the value's gone, and let this only happen once per page load. That way those of us who don't have JS don't need to keep deleting your value before typing in what we want, and those with JS only need to set focus there with a click and the input remains empty for the rest of the time the page is there. |
|
|
|
|
|
#3 |
|
SitePoint Member
Join Date: Oct 2009
Posts: 24
|
Hi Iconic_creator,
Is it something like this you want to do? <input type="text" name="myInput" value="Enter password" onfocus="if(this.value == 'Enter password'){this.value = '';}" /> I hope that helps Regards, David |
|
|
|
|
|
#4 |
|
Aiming for best practice
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2009
Location: Melbourne, Australia
Posts: 1,035
|
@Stomme poes I don't know if this still applies, but at some time some user agents had problems with empty form controls.
http://www.w3.org/TR/WCAG10-HTML-TECHS/#forms-specific I guess it's not too hard to double click the default text and delete if you don't have JavaScript enabled. With that in mind, here is a nice script courtesy of Jeremy Keith (from DOM Scripting) that allows for the behavior requested by the OP and more (this can be placed in the head section, or in a separate .js file): Code:
function resetFields(whichform) {
for (var i=0; i<whichform.elements.length; i++) {
var element = whichform.elements[i];
if (element.type == "submit") continue;
if (!element.defaultValue) continue;
element.onfocus = function() {
if (this.value == this.defaultValue) {
this.value = "";
}
}
element.onblur = function() {
if (this.value == "") {
this.value = this.defaultValue;
}
}
}
}
window.onload = prepareForms;
function prepareForms() {
for (var i=0; i<document.forms.length; i++) {
var thisform = document.forms[i];
resetFields(thisform);
}
}
All you need in the HTML is to make sure that a value is set: Code:
<input name="userName" type="text" id="uNameField" value="Enter username" maxlength="19" /> |
|
|
|
|
|
#5 | |
|
SitePoint Zealot
![]() ![]() Join Date: Aug 2008
Posts: 168
|
Quote:
I really appreciate the time and effort you have taken to help me get this going. I have everything working right now. I was provided a script by a guy called Murray at the Adobe - Dreamweaver Forum. But your code appeared to separate the JS from the HTML. Here's the JS code provided by Murray. Code:
onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue" Code:
<label for="userPassword">Password</label> <input type="text" name="userPassword" id="passwordField" onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue" value="Enter Password" maxlength="19" /> However, I will tried the method you provided also. Is whichform the name of my form? I am a novice to JS, should this be replace by the ID of my form or the form name? Thanks again. IC |
|
|
|
|
|
|
#6 | |
|
SitePoint Zealot
![]() ![]() Join Date: Aug 2008
Posts: 168
|
Quote:
This works perfectly. Code:
onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue" Thanks very much! IC |
|
|
|
|
|
|
#7 | |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,676
|
Quote:
I wouldn't want someone who accidentally skipped something to be submitting some value that isn't true, and not everyone has ease in deleting text before writing in text. |
|
|
|
|
|
|
#8 | ||
|
Aiming for best practice
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2009
Location: Melbourne, Australia
Posts: 1,035
|
@Stomme poes Strangely, the use of the default values is said to be for the sake of accessibility. As least once upon a time, I've read that browsers had trouble recognizing empty form fields, making keyboard navigation difficult, as you could not tab to empty fields.
Quote:
Quote:
|
||
|
|
|
|
|
#9 | |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,676
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 00:36.











Linear Mode
