SIGN UP MEMBER LOGIN:    
ARTICLE

TextBox Watermark Effect in JavaScript

Posted by Mahak Gupta Articles | ASP, JavaScript, CSS June 22, 2011
In this article you will see TextBox Watermark Effect in JavaScript.
Reader Level:


Step 1: Here we create a simple example of TextBox WaterMark Effect. There are two TextBoxes (txtname, txtage) in our example.

WaterMark1.gif

Step 2:

<input id="txtname" type="text" value="Enter your name here"   onblur="ShowName()" />
<
input id="txtage" type="text" value="Enter your age here" onblur="ShowAge()" /></td
>


WaterMark2.gif

Here we call two functions ShowName() and ShowAge(). These functions are useful as they check the value of the TextBox and if the TextBox has a value it will set it as TextBox value otherwise it will set the default value ("Enter your name Here" or "Enter your age here").

Step 3: Now we declare the JavaScript functions ShowName() and ShowAge():

<script language="JavaScript" type="text/javascript" >
            function ShowName() {
                var n = document.getElementById('txtname').value;
 
 
                if (n == "") {
                    document.getElementById('txtname').value = "Enter your name here";
                }
            }
            function ShowAge() {
                var a = document.getElementById('txtage').value;
                if (a == "") {
                    document.getElementById('txtage').value = "Enter your age Here ";
                }
            }
           </script
>

WaterMark3.gif

Complete Program:

<head runat="server">
    <title></title>
 
        <script language="JavaScript" type="text/javascript" >
            function ShowName() {
                var n = document.getElementById('txtname').value;

                if (n == "") {
                    document.getElementById('txtname').value = "Enter your name here";
                }
            }
            function ShowAge() {
                var a = document.getElementById('txtage').value;
                if (a == "") {
                    document.getElementById('txtage').value = "Enter your age Here ";
                }
            }
           </script
>

</head>
<
body>
    <form id="form1" runat="server">
    <div
>

        <table >
            <tr>
                <td>
                    Name</td>
                <td>
                    <input id="txtname" type="text" value="Enter your name here"   onblur="ShowName()" /></td>
            </tr>
            <tr>
                <td >
                    Age</td>
                <td>
                    <input id="txtage" type="text" value="Enter your age here" onblur="ShowAge()" /></td>
            </tr>
            </table
>

    </div>
    </form
>
</body>
</
html>

Login to add your contents and source code to this article
share this article :
post comment
 

But when i click in the input field old text remains, How i empty the field...thanks

Posted by surender bhyan Jun 23, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor