Htusa Adssad
Htusa Adssad

Reputation: 105

JavaScript - static variables?

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?

Upvotes: 0

Views: 151

Answers (1)

David G
David G

Reputation: 96865

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.

Upvotes: 2

Related Questions