Empty input box onclick in jQuery

$(this).val() = '';

should be

$(this).val('');

See .val() documentation.


Your issue is with this line of code:

$(this).val() = ''

The proper way to set a value in jQuery is:

$(this).val("Your value here");

In addition, inline JS is never a good idea. Use this instead:

$("#search").on("click", function() {
    if ($(this).val() == "search")
        $(this).val("")
});​

Here's a working fiddle.

References: jQuery .val()


It should be like this:

<input type="text" value="search" id="search" name="q" onclick="javascript:if($(this).val() == 'search') {$(this).val('');} return false;" />

correct way   : $this.val('');
incorrect way : $this.val() = '';

Put this in a separate js file...

this will return it to the initial value if nothing is typed after it loses focus.

$(document).ready( function() {
    $("#search").focus( function() {
        if ( $(this).val()=="search") {
            $(this).val('');
        } 
    });

    $("#search").blur( function() {
        if ( $(this).val()=="") {
            $(this).val('search');
        } 
    });
});