{"id":3852,"date":"2013-05-16T19:05:17","date_gmt":"2013-05-16T19:05:17","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3852"},"modified":"2013-05-16T19:05:17","modified_gmt":"2013-05-16T19:05:17","slug":"textarea-hint-using-javascript","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/textarea-hint-using-javascript\/","title":{"rendered":"Prepopulating Textarea With Hint Using JavaScript"},"content":{"rendered":"<p>Over the web, at one point of the time or the other, you may have seen websites in which a textarea shows some default value, such as &#8220;Enter your comments&#8221; and when you click inside of this textarea, this default value vanishes. This is default value is called a &#8220;Textarea Hint&#8221; as it serves the purpose of a hint and tells you what to do by doing so. Do you know how to make a textarea behave so? If not, then this article sis for you. In this article, I am going to share an easy way to <strong>show, disappear and again show textarea hint based upon\u00a0whether\u00a0an input is entered in it or not using JavaScript<\/strong>.<\/p>\n<h2>How To Prepopulate Textarea With Custom Hint Using JavaScript<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3852\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have a textarea in a form on our page. Now instead of hardcoding the value inside it, we will populate the textarea hint based upon the onfocus and onblur events. Consider the following example:<\/p>\n<h3>Example 1: Prepopulate Textarea with Hint and Lose Hint forever once any input has been entered or entered and removed<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;textarea name=\"message1\" cols=\"25\" rows=\"5\" id=\"area\" onclick=\"document.getElementById('area').innerHTML='';\" &gt;Enter your message here&lt;\/textarea&gt;<\/pre>\n<h3>Example 2: Prepopulate Textarea with Hint and Restore Default Hint even if the input has been entered \u00a0and removed<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;textarea name=\"message2\" cols=\"25\" rows=\"5\" id=\"message2\" onfocus=\"if(this.value==this.defaultValue) this.value='';\" onblur=\"if(this.value=='') this.value=this.defaultValue;\"&gt;Enter your message here&lt;\/textarea&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3852\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to <em><strong>show \/ hide textarea hint using JavaScript<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the web, at one point of the time or the other, you may have seen websites in which a textarea shows some default value, such as &#8220;Enter your comments&#8221; and when you click inside of this textarea, this default value vanishes. This is default value is called a &#8220;Textarea Hint&#8221; as it serves the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17,13],"tags":[35],"class_list":["post-3852","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tips-and-tricks","category-tips-and-tricks","tag-javascript-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-108","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3852"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3852\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}