0

I have an input type="text", its default value is "Mike". When the user clicks on it, the value is supposed to be set to '' (empty), and when it goes out of focus, its supposed to go back to "Mike" (unless the user changed the value to another name). What I've done is:

<input type="text" id="TxtFname" style="width:50%" runat="server"
    onfocus="firstText = this.text;if(this.value!=''){this.value='';}"
    onblur="if(this.value==''){this.value=firstText;}" />

and in the top of the page, in the head content place holder:

<script type="text/javascript" lang="jv">
    var firstText;
</script>

for some reason it keeps "forgetting" whats firstText's value is, so it sets the textbox value to "undefined". Is there a way to create a static variable, so firstText's value doesn't reset every time?

1
  • You really should be using the placeholder attribute for such things… Commented Mar 23, 2013 at 18:56

1 Answer 1

2

You should be setting firstText to the input's value attribute.

firstText = this.value;

Otherwise, this.text is not a defined property on the input element so its value gets defaulted to undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I thought text and value were the same thing. this solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.