Jump to content

Text box clear using onfocus


son.of.the.morning

Recommended Posts

Alright guys, i am trying to create a simple function to clear the content of a textbox. I have it working but only if i apply with in the form object, what i need is a unerversal function that can be called by more than one object.

 

This is what am using now...

<input name="Name" type="text" id="UserNameInput" value="Enter name..." class="singleLineTextBox" size="60" maxlength="100" onfocus="if(this.value == this.defaultValue){ this.value = '';}" onblur="if(this.value.length == 0){this.value = this.defaultValue;}"/>

 

...i tryied this but it just isnt having non of it

function clearText() {
       if(this.value == this.defaultValue) {
              this.value = "";
       }
       else if(this.value.length == 0) {
             this.value = defaultValue;
       }
}

//Markup
<input name="Name" type="text" id="UserNameInput" value="Enter name..." class="singleLineTextBox" size="60" maxlength="100" onfocus="clearText();"/>

Link to comment
https://forums.phpfreaks.com/topic/251774-text-box-clear-using-onfocus/
Share on other sites

What are you expecting this to be? You need to pass in the element object to the function:

 

function clearText(obj) {
       if(obj.value == obj.defaultValue) {
              obj.value = "";
       }
       else if(obj.value.length == 0) {
             obj.value = obj.defaultValue;
       }
}

 

But as I said, you need to pass in the element object when you call it. This can be done in a variety of ways...

 

clearText(document.formName.inputName);
clearText(document.getElementById('inputID'));
// etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.