Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Textarea Placeholder

I have a textarea which when in focus should empty the text holder and when out of focus should get back the original text or retain any text that was written while in focus.

EDIT:

i know how to select the text area. $("textarea").text();

i am not sure how to clear the content when u click on the textarea to nothing and then again get back the content when out of focus.

like image 599
Harsha M V Avatar asked Nov 30 '22 05:11

Harsha M V


2 Answers

Just add the placeholder parameter:

<input type="text" name="name" placeholder="input placeholder" />
<textarea name="comment" placeholder="textarea placeholder"></textarea>
like image 136
webvitaly Avatar answered Dec 06 '22 12:12

webvitaly


It seems you're trying to do the thing where you get instructions in the textarea and then if you delete the value you get the instructions again. Try this

<textarea id="a">Message</textarea>

var standard_message = $('#a').val();
$('#a').focus(
    function() {
        if ($(this).val() == standard_message)
            $(this).val("");
    }
);
$('#a').blur(
    function() {
        if ($(this).val() == "")
            $(this).val(standard_message);
    }
);

You can see it working here.

like image 37
cambraca Avatar answered Dec 06 '22 11:12

cambraca