Sign up to create your own snipts, or login.

Public snipts » ritcheyer's snipts » Clear Input using Prototype

posted on Dec 28, 2009 at 6:04 p.m. EST in 
  • <script type="text/javascript" charset="utf-8">
    	/* -------------------------------------------------------------------------------
    	As it turns out, this way isn't as efficient as initially thought. Yes, the code
    	is small and light (not as light as jQuery, but that's another topic entirely. As
    	I understand it, the 'click' and 'blur' will always fire regardless of where you
    	click on the page. If you are concerned about how many times users click on your
    	page, I suggest looking at Part II:
    	http://snipt.net/ritcheyer/clear-input-using-prototype-part-ii
    	------------------------------------------------------------------------------ */
    
    	document.observe('click',function(event) {
    		var e = event.element();
    		var defaultValue = e.value;
    		if(e.match('input[class="input"]')) {
    
    			// -- clear input field
    			e.clear();
    
    		}
    	
    		// -- if input looses focus replace value
    		e.observe('blur', function() {
    			if(e.value == '' || e.value == defaultValue) {
    				e.value = defaultValue;
    			}
    		});
    	});
    </script>
    

    copy | embed

0 Comments

Sign up, or login to leave a comment.