tags:

views:

5770

answers:

2

ive tried this but wont work.

$(function() {

    $('input[type=text]').focus(function() {

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

      });

 });
+5  A: 

To set the value, you have to pass the new value as a parameter. This is a funky thing with the .val() jQuery function.

$(this).val('')

take a look at the jQuery API and search for 'val'

Jon Erickson
lol that worked. i wonder why they did it like that.. thank
sarmenhb
no problem... i learned it the hard way as well =)
Jon Erickson
They "did it like that" because it's the only thing that remotely makes sense. What you tried to do isn't an assignment (=) in the first place, it's a comparison (==). And even if a language supports returning a value from a function that could be assigned to that way, it's a horrible idea; then if you did "var x = $(this).val(); x = 27;" you'd be changing the value of not just x, but your input, in a completely counterintuitive and insanely difficult to debug way.
chaos
thanks chaos! makes sense =)
Jon Erickson
Here is an interesting article from James Padolsey regarding getters and setters. He talks about the .val() quirkiness as well http://james.padolsey.com/javascript/fun-with-getters-setters/
Jon Erickson
A: 

A little late but another approach would be this

link text

When you click the input box it will select all the text(not clear) only if it is the default value on the box. That way if a user types something then has to go back to edit it, it's not cleared which could be quite annoying!

Also selecting the text instead of clearing it reminds the user of the example. One to keep in mind i guess.

Nooshu