Jump to content

onfocus and onblur events don't work correctly


ktsirig

Recommended Posts

Hello,

I have a page with a textbox that I want to have a message written inside it that will dissapear when the user clicks in the text box and writes something and it will show up again if the user clicks somewhere else but hasn't written anything inside the textbox. So I am using the onfocus event in order to write "Enter your email here" and the onfocus event in order to show the "Enter your email here" message inside the textbox if the user clicks somewhere else in the webpage but has left the textbox blank. If however the user has written, for example "jim@yahoo.com", I want this to remain in the textbox.

What am I doing wrong?

 

<html> 
<head> 
<script language="javascript" type="text/javascript"> 

function clearPassword() 
{  
  document.getElementById('sample').setAttribute('class', 'empty'); 
  document.getElementById('sample').value = ''; 
} 

function restorePassword()
{
  var given_text= document.getElementById('sample').value;
  
  if (given_text='')
  {
    document.getElementById('sample').setAttribute('class', 'grayedOut');
   document.getElementById('sample').value = 'Enter your email';
   } 
  else
  {
   document.getElementById('sample').setAttribute('class', 'empty');
   document.getElementById('sample').value = given_text;
  }  
} 
</script> 
</head> 
<body> 
<form > 
<input id="sample" type="text" value="Enter your email" size="22" class="grayedOut" onfocus="javascript:clearPassword();" onblur="javascript:restorePassword();"/>
<input id="new" type="text"/> 
</form> 
</body> 
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.