Results 1 to 6 of 6

Thread: in a textbox appear a start message eg "write it here" and after get focus disappear

  1. #1
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default in a textbox appear a start message eg "write it here" and after get focus disappear

    How in a textbox appear a start message eg "write it here" and after get focus disappear, and if visitor write any to textbox, never appear again ?

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Try this:
    HTML Code:
    <input type="text" value="write it here" onFocus="javascript:if(this.value=='write it here')this.value='';" onBlur="javascript:if(this.value=='')this.value='write it here';">
    Good luck!

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Here it is

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Untitled Document</title>
            <style type="text/css">
            	.gray{color: #EFEFEF}
    			.black{color: #000}        
            </style>
            <script type="text/javascript">
            	//The addEvent and removeEvent code was developed by John Resig. 
    			//I have used this code for handling the events in  a cross browser manner.
    			//http://ejohn.org/projects/flexible-javascript-events/           
                function addEvent(obj, type, fn) {
                    if (obj.attachEvent) {
                        obj['e' + type + fn] = fn;
                        obj[type + fn] = function() {
                            obj['e' + type + fn](window.event);
                        }
                        obj.attachEvent('on' + type, obj[type + fn]);
                    } else obj.addEventListener(type, fn, false);
                }         
                
                function removeEvent(obj, type, fn) {
                    if (obj.detachEvent) {
                        obj.detachEvent('on' + type, obj[type + fn]);
                        obj[type + fn] = null;
                    } else obj.removeEventListener(type, fn, false);
                }
                //John Resig's code ends here
    			
                addEvent(window,"load",function(){
    				addEvent(document.forms['f'].elements['n'],"keydown",keydownEventHandler);
    			});
    			
    			function keydownEventHandler(){				removeEvent(document.forms['f'].elements['n'],'keydown',keydownEventHandler);
    				document.forms['f'].elements['n'] .className = 'black';
    				document.forms['f'].elements['n'].value = '';
    			}
            </script>
        </head>
        <body>
        	<form name="f">
        		<input type="text" name="n" value="Write it here" class="gray"/>
        	</form>
        </body>
    </html>
    Hope this helps

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by Snookerman View Post
    Try this:
    HTML Code:
    <input type="text" value="write it here" onFocus="javascript:if(this.value=='write it here')this.value='';" onBlur="javascript:if(this.value=='')this.value='write it here';">
    Good luck!
    Two issues that I've noticed:

    1. It would be better if you avoid the usage of inline script especially if they are lengthy as it makes the markup unreadable.

    2. In your code if the person wants to enter 'write it here' itself it won't be possible. I don't have any idea about if the user wishes to avoid such a word though.

  5. #5
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Quote Originally Posted by codeexploiter View Post
    Two issues that I've noticed:

    1. It would be better if you avoid the usage of inline script especially if they are lengthy as it makes the markup unreadable.

    2. In your code if the person wants to enter 'write it here' itself it won't be possible. I don't have any idea about if the user wishes to avoid such a word though.
    1. Personal opinion. Also, not really an issue, just a suggestion.
    2. Would work just fine. Did you try it?

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    1. Personal opinion. Also, not really an issue, just a suggestion.
    I suppose we can consider the coding standards too like an opinion. Because even if you don't use them you'll get the output, right?

    2. Would work just fine. Did you try it?
    Say for example: open the page in which your input box resides. Click on the text box, the default text vanishes. Now enter the - write it here - . Next step click the text box again and see what? The text again vanishes, that means the input entered by the user is erased the second time.
    Last edited by codeexploiter; 02-20-2009 at 12:27 PM. Reason: corrections

Tags for this Thread

Bookmarks

Posting Permissions

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