0

I have the following code to fill the suchfeld <input> with "Suche...".

$(function() {
    var suchfeld = $("#suchfeld");


    suchfeld.on("focus", function() {
        if( suchfeld.val() == "Suche..." ) {
            suchfeld.val("");
        }
    });

    suchfeld.on("blur", function() {
        if( suchfeld.val() == 0 ) {
            suchfeld.val("Suche...");
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="suchfeld" type="text"/>

The problem is that after the user has the site loaded with the suchfeld <input> and the site is in the cache of browser, if the user comes back to the loaded site the <input> is empty without "Suche...".

What can I do?

Jaqen H'ghar
  • 13,728
  • 6
  • 42
  • 50

2 Answers2

1

Instead of Javascript use placeholder:

<input type="text" placeholder="Suche...">

Side note:

To hide the placeholder on focus (like in your example) use:

<input type="text" placeholder="Suche..." onfocus="this.placeholder = ''" 
     onblur="this.placeholder = 'Suche...'" >
Jaqen H'ghar
  • 13,728
  • 6
  • 42
  • 50
0

try setting on the page load the field to "Suche..."

$(function() {
    var suchfeld = $("#suchfeld");
    suchfeld.val("Suche...");

    suchfeld.on("focus", function() {
        if( suchfeld.val() == "Suche..." ) {
            suchfeld.val("");
        }
    });

    suchfeld.on("blur", function() {
        if( suchfeld.val() == 0 ) {
            suchfeld.val("Suche...");
        }
    });
});
Roxoradev
  • 853
  • 4
  • 12