CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    73

    onfocus and onblur events don't work correctly

    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 "[email protected]", I want this to remain in the textbox.
    What am I doing wrong?

    Code:
    <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>

  2. #2
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: onfocus and onblur events don't work correctly

    Code:
    <script type="text/javascript">
      function clearPassword() {
        document.getElementById('sample').className = 'empty';
        document.getElementById('sample').value = '';
      }
    
      function restorePassword() {
        var given_text = document.getElementById('sample').value;
    
        if (given_text.length < 1) {
          document.getElementById('sample').className = 'grayedOut';
          document.getElementById('sample').value = 'Enter your email';
        }
      }
    </script>
    events work fine...
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured