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
‹ CSS Background Kurzschreibweise //
IE7 Verhalten mit IE8 emulieren ›
3 Kommetare • Eintrag schreiben
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;}
});
});
nils
#2 Permalink Am 16. March 2011 um 14:14 Uhr
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"]