www.webdeveloper.com
Results 1 to 7 of 7

Thread: Input field with text that disappears when clicked

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    Cool Input field with text that disappears when clicked

    I got:
    <script type="text/javascript">
    function emptyTitle() {
    if (document.getElementById('title').value=='Enter the Title for Your Post') {
    document.getElementById('title').value=''
    }
    }
    function titleText() {
    if (document.getElementById('title').value=='') {
    document.getElementById('title').value='Enter the Title for Your Post';
    }
    }
    </script>
    ......
    <body onload="titleText();"
    ......
    <input type="text" name="post_title" size="30" tabindex="1" value="" id="title" autocomplete="off" onclick="emptyTitle();" />
    However, the text that appears is black. How do I style it to make it gray and smaller?

  2. #2
    Join Date
    Aug 2009
    Posts
    593
    Never made one of these before so here is my first attempt...

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>Example Text</title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    		<style type="text/css">
    
    			.titleText {	
    				color: #aeaeae;
    			}
    
    		</style>
    
    		<script type="text/javascript">
    
    			function titleText(id, text)
    			{
    				var element = document.getElementById(id);
    				element.onfocus = function()
    				{
    					if (this.value == text) {
    						this.value = '';
    						this.className = '';
    					}
    				};
    				element.onblur = function()
    				{
    					if (this.value == '') {
    						this.value = text;
    						this.className = 'titleText';
    					}
    				};
    				element.onblur();
    			}
    
    		</script>
    
    	</head>
    	<body>
    
    		<input id="titled1" type="text" /><script type="text/javascript">titleText('titled1', 'Example Text');</script>
    		<br /><br />
    		<input id="titled2" type="text" /><script type="text/javascript">titleText('titled2', 'Description');</script>
    		<br /><br />
    		<input id="titled3" type="text" value="Got A Value" /><script type="text/javascript">titleText('titled3', 'Previously Defaulted');</script>
    
    	</body>
    </html>
    Last edited by thraddash; 05-13-2010 at 05:50 PM.

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by narutodude000 View Post
    However, the text that appears is black. How do I style it to make it gray and smaller?
    use css to style the text to whatever size and colour you want.

  4. #4
    Join Date
    Mar 2010
    Posts
    701
    thraddash: Where did $text come from?

    Quote Originally Posted by tirna View Post
    use css to style the text to whatever size and colour you want.
    How do I style values? I want user input to be black and the onload text to be gray.

  5. #5
    Join Date
    Aug 2009
    Posts
    593
    I changed my code slightly... $text is out now.

    In my example you can just edit the .titleText css to style your onload text.

    I found changing the font size also affects the size of the input area, so I left it out.

  6. #6
    Join Date
    Mar 2010
    Posts
    701
    Thanks, instead "this.classname", I used "this.style.color='gray'"

  7. #7
    Join Date
    Aug 2009
    Posts
    593
    Then you can use this.style.color = 'inherit'; on the user inputted text.

    EDIT

    inherit didn't work like I expected, maybe just stick to black :P
    Last edited by thraddash; 05-14-2010 at 07:38 AM. Reason: After some thought...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles