son.of.the.morning Posted November 25, 2011 Share Posted November 25, 2011 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();"/> Quote Link to comment https://forums.phpfreaks.com/topic/251774-text-box-clear-using-onfocus/ Share on other sites More sharing options...
Adam Posted November 25, 2011 Share Posted November 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251774-text-box-clear-using-onfocus/#findComment-1291096 Share on other sites More sharing options...
son.of.the.morning Posted November 25, 2011 Author Share Posted November 25, 2011 Thanks man and thanks for the help with the jquery stuff the other day to i forgot to reply back to your post. Quote Link to comment https://forums.phpfreaks.com/topic/251774-text-box-clear-using-onfocus/#findComment-1291097 Share on other sites More sharing options...
son.of.the.morning Posted November 25, 2011 Author Share Posted November 25, 2011 I think you may of miss understood my post. I want to be able to re-use the function in a number of textbox objects. Quote Link to comment https://forums.phpfreaks.com/topic/251774-text-box-clear-using-onfocus/#findComment-1291101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.