Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.