Willkommen bei Schnipsel, Code Schnipsel zu Wordpress, PHP, HTML, CSS, jQuery und so weiter...

jQuery form input default value

04.2009 / von: Bastian Winkler / 3 Kommentare / Kategorie: jQuery / Tags: , , ,

Ein Defaultvalue für Input felder setzen mit jQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
jQuery.fn.DefaultValue = function() {
 
		// Scope
		var elements = this;
		var args = arguments;
		var c = 0;
 
		return(
			elements.each(function() {				
 
				// Default values within scope
				var el = $(this);
				var def = args[c++];
 
				el.val(def).focus(function() {
					if(el.val() == def) {
						el.val("");
					}
					el.blur(function() {
						if(el.val() == "") {
							el.val(def);
						}
					});
				});
 
			})
		);
	};
 
$(document).ready(function(){
       $("input").DefaultValue("Suchen");
});

Statistik: read: 5305 / today: 5 / last: 16.05.2012
//

  1. 3 Kommetare • Eintrag schreiben

  2. F.U.T.Z.
    #1 Permalink Am 14. December 2010 um 22:07 Uhr

    Au schick, hat bestimmt ne weile gedauert das so ordendlich zu basteln. Nur versteh ich nicht wozu das ganze als jQuery Plugin sein muss?

    Reicht da nicht einfach das hier aus:

    $(document).ready(function() {
    $(“textarea”).focus(function(){
    if(this.value==this.defaultValue){this.value=”;}
    });
    $(“textarea”).blur(function(){
    if(this.value==”){this.value=this.defaultValue;}
    });
    });

  3. nils
    #2 Permalink Am 16. March 2011 um 14:14 Uhr

    
          
  4. nils
    #3 Permalink Am 16. March 2011 um 14:15 Uhr

    [input value="click here" onfocus="value=='click here'?value='':0" onblur="value==''?value='click here':0"]

Kommentar schreiben

Code einfügen: <pre lang="php" line="1"> Code hier </pre>