{"id":109,"date":"2008-11-15T14:33:26","date_gmt":"2008-11-15T19:33:26","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=109"},"modified":"2008-11-15T20:15:20","modified_gmt":"2008-11-16T01:15:20","slug":"javascript-textarea-counter","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2008\/11\/javascript-textarea-counter\/","title":{"rendered":"Javascript textarea counter"},"content":{"rendered":"<p>I&#8217;ve been thinking more about the textarea counter issue that I mentioned in my previous post (&#8220;<a href=\"https:\/\/osric.com\/chris\/accidental-developer\/?p=103\">Users Paste Differently<\/a>&#8220;).<\/p>\n<p>First of all, I noticed that some of the textarea counter scripts date back to at least 2000, so this has been a problem that developers have been looking to solve for 8 years. I checked the <a href=\"http:\/\/www.w3.org\/html\/wg\/html5\/\">HTML 5 specification<\/a> and found that in HTML 5, the <a href=\"http:\/\/www.w3.org\/html\/wg\/html5\/#the-textarea-element\">textarea element has a maxlength attribute<\/a>. Presumably user agents will build in the most elegant solution.<\/p>\n<p>But what is the current most elegant solution?<!--more--><br \/>\nI checked a couple sites that I know use textarea counters well: <a href=\"http:\/\/delicious.com\/\">Delicious<\/a> and <a href=\"http:\/\/twitter.com\/\">Twitter<\/a>.<\/p>\n<p><strong>Delicious<\/strong><br \/>\n<figure id=\"attachment_118\" aria-describedby=\"caption-attachment-118\" style=\"width: 410px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/delicious-textarea-counters.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/delicious-textarea-counters.png\" alt=\"Textarea counters on Delicious.com\" title=\"delicious-textarea-counters\" width=\"410\" height=\"66\" class=\"size-full wp-image-118\" srcset=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/delicious-textarea-counters.png 410w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/delicious-textarea-counters-300x48.png 300w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/a><figcaption id=\"caption-attachment-118\" class=\"wp-caption-text\">Textarea counters on Delicious.com<\/figcaption><\/figure><\/p>\n<p><strong>Twitter<\/strong><br \/>\n<figure id=\"attachment_120\" aria-describedby=\"caption-attachment-120\" style=\"width: 384px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/twitter-textarea-counters.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/twitter-textarea-counters.png\" alt=\"Textarea counters on Twitter.com\" title=\"twitter-textarea-counters\" width=\"384\" height=\"80\" class=\"size-full wp-image-120\" srcset=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/twitter-textarea-counters.png 384w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/twitter-textarea-counters-300x62.png 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/a><figcaption id=\"caption-attachment-120\" class=\"wp-caption-text\">Textarea counters on Twitter.com<\/figcaption><\/figure><\/p>\n<p>I like how prominent the character count is in Twitter, although perhaps too terse, but if Javascript is disabled it displays nothing at all. On the other hand, in Delicious displays &#8220;1000 characters max&#8221; if Javascript is off (instead of &#8220;1000 characters left&#8221;), which is still useful information. (I would avoid using the abbreviation <em>max<\/em>, though, and use <em>maximum<\/em> or <em>limit<\/em> instead, which might be better understood by those for whom English is a second language.)<\/p>\n<p>I like Twitter&#8217;s &#8220;warning track&#8221; that lets the user know they need to keep it concise and wrap it up, although I find it confusing that they used bright red to denote both the &#8220;dangerously close to&#8221; as well as &#8220;over&#8221; the limit. The positive bright red numbers could easily be misconstrued as over the limit, since red is a color we frequently use to denote errors.<\/p>\n<p>Twitter&#8217;s counter responds to onBlur and onChange, whereas the counter in Delicious is triggered by neither.<\/p>\n<p>Both get one thing right that I think most other textarea counters get wrong: they don&#8217;t truncate the user&#8217;s text. Deleting something that the user has typed (or pasted) is definitely a bad idea for at least two reasons: the user may not realize the input was truncated and may submit incomplete info, and that the user, upon discovering that the input requires editing, may choose to cut text from someplace other than the end. Instead, both versions alert the user that they are over the limit and provide information on how many characters need to be cut to stay within the limit.<\/p>\n<p>(As an aside, one thing I find curious is that both count an <em>Enter<\/em> keystroke as one character. I&#8217;ve run into issues with this before because a line break on a Windows-based system should insert 2 ASCII characters: a Carriage Return (CR) and a Line Feed (LF). *nix systems, including OS X, will insert a Line Feed (LF). However, I haven&#8217;t been able to reproduce this issue with recent testing. If it is still an issue, I imagine they handle this on the back-end by converting the CRLF to LF before inserting it into the database?)<\/p>\n<p>In short, my recommendations are:<\/p>\n<ul>\n<li>Don&#8217;t truncate the user&#8217;s input; let the user correct it<\/li>\n<li>Let the user know how far over the limit he or she is<\/li>\n<li>Provide guidelines even if Javascript is disabled<\/li>\n<li>onBlur and onChange should also trigger the counter, in case the user is pasting text<\/li>\n<li>Keep the counter message simple and clear<\/li>\n<\/ul>\n<p>Another idea I had for a visual counter:<br \/>\n<figure id=\"attachment_116\" aria-describedby=\"caption-attachment-116\" style=\"width: 250px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/counter-bar.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/11\/counter-bar.png\" alt=\"Counter - characters remaining\" title=\"counter-bar\" width=\"250\" height=\"20\" class=\"size-full wp-image-116\" \/><\/a><figcaption id=\"caption-attachment-116\" class=\"wp-caption-text\">Counter - characters remaining<\/figcaption><\/figure><br \/>\nAside from turning red once past the limit, though I&#8217;m not sure how it would visually convey to the user by how much the text exceeds the limits.<\/p>\n<p><strong>How can a counter be implemented programatically?<\/strong><br \/>\nI would like to set something up so that the appropriate event handlers are attached to textarea elements automatically. I found a <a href=\"http:\/\/jquery.com\/\">jQuery<\/a> plugin, <a href=\"http:\/\/plugins.jquery.com\/project\/CharLimit\">Char Limit<\/a>, but in addition to not meeting the recommendations I made above, I don&#8217;t like how the developer has to specify the character limit in the Javascript (and apply the same limit for all textarea elements).<\/p>\n<p>I think a clever, although imperfect, solution would be to have jQuery (or any Javascript of your choice) turn any static character limit messages (like the one that Delicious provides if Javascript is disabled) into dynamic character counters. For example, with the following code, the Javascript could find any elements with id attributes matching *_counter, take the numeric part as the limit, and build a character counter:<br \/>\n<code>&lt;textarea name=\"example\" id=\"example\"&gt;&lt;\/textarea&gt;<br \/>\n&lt;div id=\"example_counter\"&gt;300 characters maximum&lt;\/div&gt;<\/code><\/p>\n<p>It&#8217;s problematic for several reasons: another element on the page could have an id that matches *_counter, the non-Javascript message could  contain more than one numeric part, and the developers would have be consistent in naming the textareas and accompanying divs. But I think it is in many ways an improvement over the inline Javascript I&#8217;ve seen in some oft-used solutions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been thinking more about the textarea counter issue that I mentioned in my previous post (&#8220;Users Paste Differently&#8220;). First of all, I noticed that some of the textarea counter scripts date back to at least 2000, so this has been a problem that developers have been looking to solve for 8 years. I checked &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2008\/11\/javascript-textarea-counter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript textarea counter<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8,27],"tags":[39,38,350,41,37,354],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-best-practices","category-javascript","category-user-interface","tag-delicious","tag-forms","tag-javascript","tag-jquery","tag-textarea","tag-twitter"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":11,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/109\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}