Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Making a Initial Text Value Vanish one selected

    Code:
    <input type="text" name="name" size="20" maxlength="30" value="Name"/>
    on clicking the above field how can i make the value Name vanish ??

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,635
    Thanks
    6
    Thanked 1,003 Times in 976 Posts
    With JavaScript only:

    Code:
    <input type="text" name="name" size="20" maxlength="30" value="Name"  onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" />

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanx mate...

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    48
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thank you Thank you Thank you
    You are the best. Nice, clean code too - works with other events perfectly =]

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    48
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Just out of interest...

    Say there were multiple text inputs with different values.
    Would you need a set of the code for each one or is the a var option for that, like 'get the value'?

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    48
    Thanks
    10
    Thanked 0 Times in 0 Posts
    oops replied in the wrong thread - my bad
    ill repost in the correct one =[

  • #7
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    This code is a bit better because they can now enter in as input whatever the default text is (this can come in handy if you're giving a default date or first name or whatever):
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Example</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // <![CDATA[
    
    function clearOnce(input_element)
    	{
    	if (!input_element.alreadyCleared)
    		{
    		input_element.value = "";
    		input_element.alreadyCleared = true;
    		}
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <form onsubmit="return false">
    	<input type="text" onfocus="clearOnce(this)" value="type stuff here" /><br />
    	<input type="text" onfocus="clearOnce(this)" value="and here" />
    </form>
    
    </body>
    </html>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by itsallkizza
    This code is a bit better because they can now enter in as input whatever the default text is (this can come in handy if you're giving a default date or first name or whatever):
    but that won't retain the original text when the user deletes everything s/he typed.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    I wasn't commenting on the given onblur - that one was fine, although modularizing it into a central function would be preferable.

    The onblur function to me seems a bit more case-by-case-ish anyway. In most of the forms I create, I wouldn't want the default value to pop back in if the user deleted the input text. Whether you want it to do that or not is up to the developer.
    Last edited by itsallkizza; 12-31-2008 at 04:49 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #10
    New Coder
    Join Date
    Sep 2009
    Posts
    51
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Thanks, Thats great!!!


  •  

    Posting Permissions

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