Reputation: 48798
I've no idea why this isn't working, but I'm sure it's a very common bit of Javascript. My syntax is clearly lacking, and I don't know why:
<input type="text" value="search" id="search" name="q" onclick="javascript:if($(this).val() == 'search') {$(this).val() = '';} return false;" />
Upvotes: 9
Views: 59164
Reputation: 11
This has become easier with a HTML5 Placeholder:
<input type="text" name="search" placeholder="Enter your search query here">
It's not supported in older browsers, but if you're only targeting modern ones, you should be fine to use it: http://caniuse.com/#feat=input-placeholder
Upvotes: 1
Reputation: 6463
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() = '';
Upvotes: 3
Reputation: 12998
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');
}
});
});
Upvotes: 4
Reputation: 16743
$(this).val() = '';
should be
$(this).val('');
See .val() documentation.
Upvotes: 2
Reputation: 61832
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()
Upvotes: 15
Reputation: 4764
function search(textBox)
{
if($(textBox).val() == 'search') {
$(textBox).val() = '';
}
return false;
}
<input type="text" value="search" id="search" name="q" onclick="return search();" />
Try this and debug.
Upvotes: -2
Reputation: 8424
no you need $(this).val(""); or $(this).val(''); to empty it
<input type="text" value="search" id="search" name="q" onclick="javascript:if($(this).val() == 'search') {$(this).val('');} return false;" />
Upvotes: 0