Abhishek Zirota Abhishek Zirota - 3 years ago 99
Javascript Question

Javascript code: I want the text box to be empty when on focus

I want the text dissappear when the username box is clicked, am I doing it right with onfocus? what am I doing wrong, please help.

document.getElementById("username").onfocus = function() {
document.getElementById("username").value = "";

<form name="Sign Up">
<legend><i><b>Siqn Up</b></i>
<label for="username">Username</label>
<input type="text" value="Unique ID" name="username">
<label for="email">Email</label>
<input type="text" name="email">
<label for="password">Password
<input type="password" name="password">
<input type="submit" value="Sign Up">

Answer Source

The text box only has a name of "username". You need to give it an id attribute as well.

<input type="text" value="Unique ID" name="username" id="username">

Notice how the javascript function is called getElementById? :) That means it needs an Id!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download