{"id":225,"date":"2011-05-19T16:35:29","date_gmt":"2011-05-19T16:35:29","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=225"},"modified":"2011-05-19T16:55:59","modified_gmt":"2011-05-19T16:55:59","slug":"labels-inside-form-fields-with-jquery","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/labels-inside-form-fields-with-jquery\/","title":{"rendered":"Labelling form fields without using labels &#8211; Method II"},"content":{"rendered":"<p>Here&#39;s a handy method I came up with to &quot;move&quot; labels inside form fields, to save real estate. It also degrades gracefully if the user does not support JavaScript. It depends on jQuery.<\/p>\n<h3>Example form<\/h3>\n<p><code>&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;div class=&quot;field&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;label for=&quot;name&quot;&gt;Name&lt;\/label&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; \/&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;\/div&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;div class=&quot;field&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;label for=&quot;email&quot;&gt;Email&lt;\/label&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; \/&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;\/div&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;div class=&quot;field&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;label for=&quot;message&quot;&gt;Questions&lt;\/label&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;textarea name=&quot;message&quot; id=&quot;message&quot;&gt;&lt;\/textarea&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;\/div&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;div class=&quot;form-controls-no-js&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;input type=&quot;image&quot; src=&quot;\/img\/submit.png&quot; alt=&quot;Submit&quot; \/&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;\/div&gt;<br \/>\n\t&lt;\/form&gt;<\/code><\/p>\n<h3>&nbsp;<\/h3>\n<h3>jQuery<\/h3>\n<p><code>$(&#39;form label&#39;).each(function() { $(this).next().val($(this).html()); $(this).hide(); });<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>To take it a step further, if we want these prefilled values to be wiped out when the input takes the focus:<\/p>\n<p><code>$(&#39;form label&#39;).each(function() {<br \/>\n\t&nbsp;&nbsp;&nbsp; $(this).hide();<br \/>\n\t&nbsp;&nbsp;&nbsp; $(this).next().val($(this).html());<br \/>\n\t&nbsp;&nbsp;&nbsp; $(this).next().focus(function() { if($(this).val() == $(this).prev().html()) $(this).val(&#39;&#39;); });<br \/>\n\t});<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#39;s a handy method I came up with to &quot;move&quot; labels inside form fields, to save real estate. It also degrades gracefully if the user&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5,25],"tags":[37],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/225"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}