简体   繁体   中英

javascript: onblur value not reappearing

I'm trying to get 'Name' to reappear in this input box if the user take the focus away from the box without typing anything.

This is the code that is not working:

<input type="text" value="Name" onfocus="if(this.value='Name'){this.value=''}"  onblur="if(this.value=''){this.value='Name'}"/>

Hoping someone can help me out quickly with it!

As pointed out in the comments by Rob Sedgwick , use === for comparing instead of = ,

<input type="text" value="Name" onfocus="if(this.value==='Name'){this.value=''}" onblur="if(this.value===''){this.value='Name'}" />

PS You can use placeholder for this:

<input type="text" placeholder="Name" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM