Geeth
Geeth

Reputation: 5343

Background text in textbox

I want to display a background text in the textbox.

ex: The Title textbox in the stackoverflow Ask question page.

Needs: If i enter some text inside the textbox it should disappear.

Geetha.

Upvotes: 3

Views: 7652

Answers (3)

vitorbal
vitorbal

Reputation: 3031

Well, since you put jquery as a tag for the question, I assume you're already using jquery for your project. In that case, here's a neat plugin that does the trick: Labelify. Good luck!

Upvotes: 1

Jinal Desai - LIVE
Jinal Desai - LIVE

Reputation: 336

You need to use javascript for the same.

Use following two functions in javascript and it is done.

        function clearDefault(obj,val)
        {
            objInput=document.getElementById(obj);
            if(objInput.value==val)
            {
                objInput.value="";
            }
        }
        function setDefault(obj,val)
        {
            objInput=document.getElementById(obj);
            if(objInput.value=="")
            {
                objInput.value=val;
            }
        }

You need to apply it on your code by using two javascript events as follow.

    <input name="email" type="text" class="textbox" id="email" 
  value="Email" size="12" onfocus="clearDefault('email','Email');" 
  onblur="setDefault('email','Email');"/>

So when you control get focus it the default text will be cleared. Ant That's it.

Hope this will help!

Upvotes: 1

Related Questions