본문 바로가기

Story/Javascript

input 상자안에 기본값을 설정하고 focus가 들어왔을때 기본값을 없애주는 방법

반응형

JavaScript Clear Textfield Default Value onClick

So basically if you have a text field with default text like “Enter Email Address Here” and you want it so that when the user clicks in it, the text disappears, this is the code to use. It goes one step further so that if you do not enter anything it puts the default text back and if there is something entered it doesn’t get tripped up and clear it out. Plus it is super simple and clean and tested in all browsers.

Here is the code:

function clearText(field){

    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;

}

Here is how to use it:

<input name="emailaddress" type="text"  value="Enter Your Email Address" onFocus="clearText(this)" onBlur="clearText(this)">

출처 http://www.bradino.com/javascript/clear-textfield-default-value-onclick/
반응형