views:

55

answers:

4

I have 3 textarea with a default text "Write something". Let say if i click on one of them, the default text will disappear and that textarea get focus. However, if i click on another textarea, the default text of another textarea will disappear but the default text of first textarea will reappear. How can I make this happen? Same thing is that if I am to click on the third textarea, its default text of the textarea that I clicked on will disappear and the other will reappear.

Coding:

<textarea class="t" id="tt3">Write something</textarea>
<textarea class="t" id="tt3">Write something</textarea>
<textarea class="t" id="tt3">Write something</textarea>

How can I do that with jquery?

Thank you.

+1  A: 

What you are talking about (I think) is often referred to as a watermark.

Take a look at this: http://code.google.com/p/jquery-watermark/

fehays
How if it is not only apply on the default text, but it will change the style of textarea too when it got focus and blur? Thanks
limfreak
jquery has focus and blur events. take a look at the jquery.com documentation.
fehays
A: 

Consider using the HTML5 placeholder attribute and falling back on a javascript solution if the browser doesn't support that attribute.

Stefan Kendall
How if it is not only apply on the default text, but it will change the style of textarea too when it got focus and blur? Thanks.
limfreak
The style? How do you mean? It will do the behavior you described in your original post.
Stefan Kendall
A: 

This should do it for ya!

http://www.jsfiddle.net/86TTc/1/

I've tested this across a fairly wide-range of browsers (the same ones that jQuery supports). I may have missed one or two, but this should get you 99.9% of the way there.

I hope this helps you both with your specific task, and helps you understand how it is done.

mkoistinen
@Stefan Kendall, I guess I'm incompetent since I didn't use a plugin, eh?
mkoistinen
-1 for lack of Modernizr and libraries to support this behavior. Your code is not re-usable or maintainable. This should obviously be handled by a library; the least you could have done would be write your code in plugin form, but even then it's shadier than just using an existing solution.
Stefan Kendall
Loading a whole library for one line of code? No thanks. Your opinions have little bearing here. I've addressed the request *exactly* and you actively penalise me for it? And, you haven't even posted a solution yourself, just an idea. I think you've lost the plot.
mkoistinen
I'll go further to that your behaviour here makes SO less than it could be.
mkoistinen
+1  A: 

First, you declare a css with some classes:

.t {
    font-family: Helvetica, Geneva, Arial, sans-serif;
}

.txt-selected {
    font-style: normal;
}

.txt-unselected {
    font-style: italic;
}

Then, in javascript you write:

$(document).ready(function () {

    var defaultText = "write something";

    $(".t")
        .addClass("txt-unselected")
        .val(defaultText)
        .focus(function () {

            var $this = $(this);

            if($this.val() == defaultText)
            {
                $this.val("");
                $this.removeClass("txt-unselected");
                $this.addClass("txt-selected");
            }
        })
        .blur(function () {

            var $this = $(this);

            if($this.val() == "")
            {                
                $this.removeClass("txt-selected");
                $this.addClass("txt-unselected");
                $this.val(defaultText);
            }
        });
});

That should do the work. ;)

Pato