Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background text in textbox

Tags:

jquery

c#

asp.net

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.

like image 774
Geeth Avatar asked Jul 09 '26 01:07

Geeth


2 Answers

A good link is http://attardi.org/labels as referenced here https://stackoverflow.com/questions/781473/how-to-create-a-label-inside-an-input-element

like image 115
TheGeekYouNeed Avatar answered Jul 11 '26 15:07

TheGeekYouNeed


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!

like image 38
Jinal Desai - LIVE Avatar answered Jul 11 '26 13:07

Jinal Desai - LIVE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!