Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Blinking Text in TextBox using Javascript

5.00/5 (2 votes)
24 Nov 2011CPOL 27.5K  
Blinking Text in TextBox using Javascript

Description


After seeing this question[^] in Q/A section, I just created this thing in Javascript & now posting here.

Code


XML
<html>
<head>
<title>Blinking Text in TextBox using Javascript</title>
<script language="javascript" type="text/javascript">
var timer;
function BlinkingText()
{
 if(document.getElementById("txtName").value == "")
 {
   document.getElementById("txtName").value = "Enter your name..";
 }
 else
 {
   document.getElementById("txtName").value = "";
 }
 timer = setTimeout("BlinkingText()", 500);
}

function StopBlinking()
{
  clearTimeout(timer);
}

function ContinueBlinking()
{
  if(document.getElementById("txtName").value == "Enter your name.." || document.getElementById("txtName").value == "")
  {
    BlinkingText();
  }
}

function DoFocus()
{
  if(document.getElementById("txtName").value == "Enter your name.." || document.getElementById("txtName").value == "")
  {
    document.getElementById("txtName").value = "";
    StopBlinking();
  }
}
</script>
</head>

<body onload="BlinkingText()">
Name : <input type="text" id="txtName" value="Enter your name.." onfocus="DoFocus();" onblur="ContinueBlinking();" />
</body>
</html>

Browser Compatibility


I have tested this script in the following Web browsers:

  • Internet Explorer
  • Mozilla Firefox
  • Google Chrome
  • Safari
  • Opera

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)