SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onlick delete value

    Hi Guys,

    i have a textarea with a default value. when it is clicked the default value clears, but when it is left blank i want it to reset to the default value again, unless the end user types somthing.

    at the moment i am using onclick="this.value=' ' "

    but its not enough for what i want to do.

    thanks
    dug

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    window.onload = function (){
    	var inputs = document.getElementsByTagName ('input');
    	var textareas = document.getElementsByTagName ('textarea');
    	toggleIt (inputs);
    	toggleIt (textareas);
    }
     
    function toggleIt (collection){
    	for (var i=0,j; j = collection[i]; i++){
    		if (j.nodeName.toLowerCase() == 'textarea' || (j.nodeName.toLowerCase() == 'input' && j.getAttribute ('type')=='text')){
    			if (j.value.length != 0){
    				j.__DEFAULTVALUE__ = j.value;
    				j.onfocus = function (){
    					if (this.value === this.__DEFAULTVALUE__){
    						this.value = '';
    					}
    					return false;
    				}
    				j.onblur = function (){
    					if (this.value.length == 0){
    						this.value = this.__DEFAULTVALUE__;
    					}
    					return false;
    				}
    			} else continue;
    		}
    	}
    }

    You can use this script.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ize View Post
    Code javascript:
    window.onload = function (){
    	var inputs = document.getElementsByTagName ('input');
    	var textareas = document.getElementsByTagName ('textarea');
    	toggleIt (inputs);
    	toggleIt (textareas);
    }
     
    function toggleIt (collection){
    	for (var i=0,j; j = collection[i]; i++){
    		if (j.nodeName.toLowerCase() == 'textarea' || (j.nodeName.toLowerCase() == 'input' && j.getAttribute ('type')=='text')){
    			if (j.value.length != 0){
    				j.__DEFAULTVALUE__ = j.value;
    				j.onfocus = function (){
    					if (this.value === this.__DEFAULTVALUE__){
    						this.value = '';
    					}
    					return false;
    				}
    				j.onblur = function (){
    					if (this.value.length == 0){
    						this.value = this.__DEFAULTVALUE__;
    					}
    					return false;
    				}
    			} else continue;
    		}
    	}
    }

    You can use this script.
    Thanks Ize, i would not have thought it be this complicated....

    dug

  4. #4
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ma201dq View Post
    Thanks Ize, i would not have thought it be this complicated....

    dug
    Oh it's not complicated, believe me
    The script is pretty basic, but it's a little more code because it automatically adds that functionality to every input-field or textarea on a page.


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
  •