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?

3

2 Answers 2

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...'" >

Sign up to request clarification or add additional context in comments.

Comments

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...");
        }
    });
});

2 Comments

The problem is that there is no reload when the user comes back to the page. This example doesn't solve it!
Why don't you try using a placeholder instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.