1

When a textbox is created, default, gray text (#888) is given a displayed. When it is given focus, the value should disappear and start showing the typed value. I've written the code for this problem and it is as follows:

<html>
    <script type="text/javascript">
        function Focus(i) {
            if (i.value == i.defaultValue) {
                i.value = "";
                i.style.color = "#000";
            }
        }

        function Blur(i) {
            if (i.value == "") {
                i.value = i.defaultValue;
                i.style.color = "#888";
            }
        }
    </script>

    <body>
        <input type="text" name="enter firstname" title="First Name" style="color:#888;"
        value="First Name" onfocus="Focus(this)" onblur="Blur(this)" />
        <input type="text" name="enterlastname" title="Last Name" style="color:#888;"
        value="lastname" onfocus="Focus(this)" onblur="Blur(this)" />
    </body>

</html>

But, here whenever the textbox is focused, the value is disappearing. What should I do in order to correct this? Even though the text box is under focus, the value should not disappear and the value should disappear only when I start typing in it. I'm a new user so I can't post screenshots.

6
  • With jsfiddle.net you could get answers faster.. Commented Oct 23, 2012 at 12:13
  • Firstly script tags not inside body/head elements create invalid HTML and might even cause some exceptions in some browsers. Commented Oct 23, 2012 at 12:15
  • i.defaultValue is undefined but I'd go for the placeholder solution => pretty easy Commented Oct 23, 2012 at 12:19
  • hey, i din het u @sujathan . What is wrong in placing script tags inside body/head?? Commented Oct 23, 2012 at 12:56
  • first read the comment properly Commented Oct 23, 2012 at 13:02

3 Answers 3

5

what you want is called placeholder.

use it like this:

<input type="text" name="enterfirstname" placeholder="First Name" />
<input type="text" name="enterlastname" placeholder="Last Name" />
Sign up to request clarification or add additional context in comments.

3 Comments

more info about placeholder
If you want pre HTML5 support for the placeholder attribute i can recommend the jQuery Placeholder plugin (github.com/danielstocks/jQuery-Placeholder).
placeholder is a good idea, but it isn't supported by IE, so he will still need something in place to accommodate for IE users.
0

Try with

<html>
<head>
<script type="text/javascript">
    function Focus(i) {
        if (i.value == i.defaultValue) {
            i.value = "";
            i.style.color = "#000";
        }
    }

    function Blur(i) {
        if (i.value == "") {
            i.value = i.defaultValue;
            i.style.color = "#888";
        }
    }
</script>
</head>
<body>
    <input type="text" name="enter firstname" title="First Name" style="color:#888;"
    value="First Name" onfocus="Focus(this)" onblur="Blur(this)" />
    <input type="text" name="enterlastname" title="Last Name" style="color:#888;"
    value="lastname" onfocus="Focus(this)" onblur="Blur(this)" />
</body>

</html>

Note:script tags not inside body/head elements create invalid HTML

1 Comment

I did not get what i expected. Please refer this link, It shows my quetion mozilla.org/en-US/contribute Under, WANT TO HELP, the text box has my question
0

Try using "onkeydown" as following...

function Focus(cntrl,value)
{
    if (cntrl.value == value)
    {
       cntrl.value           = '';
       cntrl.style.color     = '#000';
    }
} 
function Blur(cntrl,value)
{
    if (cntrl.value == '') 
    {
       cntrl.style.color     = 'gray';
       cntrl.value           = value;                         
    }
}

<body>
     <input type="text" name="enter firstname" title="First Name" style="color:#888;"      value="First Name" onkeydown="Focus(this,'First Name')" onblur="Blur(this,'First Name')" />
     <input type="text" name="enterlastname" title="Last Name" style="color:#888;"      value="lastname" onkeydown="Focus(this,'lastname')" onblur="Blur(this,'lastname')" />
</body>

1 Comment

Thanks, I got the answer, but when the cursor is placed, the cursor is at last position, i.e at last letter. What should i do, to place the cursor at the 1st position

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.