I am working on simple login form.
I have two text fields as user name and password.
user name text field have hint as “user name”.
password text field have hint as “password”.
I want to hide hint text while clicking inside textbox.
here i faced two problems,
-
if i click inside user name text box the hint text is hiding. but when i press ctrl+z then again the hint appearing and able to edit by pressing keyboard arrow keys.
-
second one is the password text field type is password. so it is not showing hint text as “password”.
here is my code:
<html>
<head>
<title>login</title>
<script>
function ForUserName()
{
if(document.getElementById('username').value == "user name")
{
document.getElementById('username').value='';
}
else if(document.getElementById('username').value == '')
{
document.getElementById('username').value='user name';
}
}
function ForPassword()
{
if(document.getElementById('password').value == "password")
{
document.getElementById('password').value='';
}
else if(document.getElementById('password').value == '')
{
document.getElementById('password').value='password';
}
}
</script>
</head>
<body>
<input type="text" id="username" value="user name" onclick="ForUserName();" onblur="ForUserName();">
<input type="password" id="password" value="password" onclick="ForPassword();" onblur="ForPassword();">
</body>
</html>
can any one please…
There is a HTML5 attribute that does that called
placeholder. So you just do:And for the backward compatibility (older browsers) you just follow this tutorial.