{"id":1165,"date":"2018-11-22T01:18:22","date_gmt":"2018-11-21T22:18:22","guid":{"rendered":"http:\/\/elzero.org\/?p=1165"},"modified":"2021-07-20T22:02:43","modified_gmt":"2021-07-20T19:02:43","slug":"javascript-calculate-textarea-characters","status":"publish","type":"post","link":"https:\/\/elzero.org\/old\/javascript-calculate-textarea-characters\/","title":{"rendered":"\u0637\u0631\u064a\u0642\u0629 \u062d\u0633\u0627\u0628 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u062f\u0627\u062e\u0644 \u0627\u0644 Textarea"},"content":{"rendered":"<div class=\"text\">\r\n\u0637\u0631\u064a\u0642\u0629 \u062d\u0633\u0627\u0628 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u062f\u0627\u062e\u0644 \u0627\u0644 Textarea \u0628\u0633\u064a\u0637\u0629 \u062c\u062f\u0627, \u0646\u0642\u0648\u0645 \u0628\u0639\u0645\u0644 Form \u062c\u062f\u064a\u062f\u0629 \u0648\u0628\u062f\u0627\u062e\u0644\u0647\u0627 Textarea \u0648\u0623\u0633\u0641\u0644\u0647\u0627 Span \u0644\u0646\u0636\u0639 \u0641\u064a\u0647\u0627 \u0627\u0644\u0631\u0642\u0645 \u0627\u0644\u062e\u0627\u0635 \u0628\u0627\u0644\u0639\u062f\u0627\u062f.\r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-markup\"><form>\r\n  <textarea id=\"text\" rows=\"8\" cols=\"80\" maxlength=\"100\"><\/textarea>\r\n  <span id=\"count\">100<\/span>\r\n<\/form><\/script>\r\n<div class=\"text\">\r\n\u0628\u0639\u062f \u0630\u0644\u0643 \u0646\u0642\u0648\u0645 \u0628\u0639\u0645\u0644 \u0645\u062a\u063a\u064a\u0631\u0627\u062a \u0644\u0644\u0639\u0646\u0627\u0635\u0631 \u0627\u0644\u062a\u064a \u0633\u0648\u0641 \u0646\u0639\u0645\u0644 \u0639\u0644\u064a\u0647\u0627 \u0648\u0647\u064a \r\n<\/div>\r\n<ul class=\"points\"> \r\n  <li>\u0627\u0644 Span \u0627\u0644\u062a\u064a \u062a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 \u0627\u0644\u0639\u062f\u062f<\/li>\r\n  <li>\u0627\u0644 Textarea \u0646\u0641\u0633\u0647\u0627 <\/li>\r\n  <li>\u0623\u0642\u0635\u0649 \u0639\u062f\u062f \u0644\u0644\u062d\u0631\u0648\u0641 \u064a\u062a\u0645 \u062c\u0644\u0628\u0647 \u0645\u0646 \u0627\u0644 Attribute \u0627\u0644\u0645\u0633\u0645\u0649 maxlength \u0639\u0646 \u0637\u0631\u064a\u0642 \u062f\u0627\u0644\u0629 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/getAttribute\" rel=\"noopener\" target=\"_blank\">getAttribute<\/a><\/li>\r\n<\/ul>\r\n\r\n<script type=\"text\/plain\" class=\"language-javascript\">\r\nvar $count = document.getElementById('count'),\r\n    $textarea = document.getElementById('text'),\r\n    $maxLength = $textarea.getAttribute('maxlength');\r\n<\/script>\r\n<div class=\"text\">\r\n\u0627\u0644\u0622\u0646 \u0646\u0642\u0648\u0645 \u0628\u0625\u0633\u062a\u0639\u0645\u0627\u0644 Event \u0627\u0644\u0645\u0633\u0645\u0627\u0629 oninput \u062d\u062a\u0649 \u0646\u062a\u0645\u0643\u0646 \u0645\u0646 \u062d\u0633\u0627\u0628 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u0639\u0646\u062f \u0648\u0636\u0639 \u0623\u064a \u0634\u064a\u0621 \u062f\u0627\u062e\u0644 \u0627\u0644 Textarea \u0633\u0648\u0627\u0621 \u0643\u062a\u0627\u0628\u0629 \u0627\u0648 \u0639\u0646 \u0637\u0631\u064a\u0642 \u0627\u0644\u0644\u0635\u0642 Ctrl + V\r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-javascript\">\r\n$textarea.oninput = function () {\r\n  \r\n};\r\n<\/script>\r\n<div class=\"text\">\r\n\u0628\u0639\u062f \u0630\u0644\u0643 \u0646\u0628\u062f\u0623 \u0628\u062d\u0633\u0627\u0628 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u0627\u0644\u062a\u064a \u064a\u062a\u0645 \u0643\u062a\u0627\u0628\u062a\u0647\u0627 \u0623\u0648 \u0644\u0635\u0642\u0647\u0627 \u062f\u0627\u062e\u0644 \u0627\u0644 Textarea \u0639\u0646 \u0637\u0631\u064a\u0642 \u0639\u0645\u0644 \u0639\u0645\u0644\u064a\u0629 \u062d\u0633\u0627\u0628\u064a\u0629 \u062a\u0642\u0648\u0645 \u0628\u0637\u0631\u062d \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u0627\u0644\u0645\u0648\u062c\u0648\u062f\u0629 \u062f\u0627\u062e\u0644 \u0627\u0644 Textarea \u0645\u0646 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 \u0627\u0644\u0643\u0644\u064a \u0627\u0644\u0645\u0633\u0645\u0648\u062d \u0628\u0647 \u0627\u0644\u0645\u0648\u062c\u0648\u062f \u062f\u0627\u062e\u0644 maxlength Attribute \u062b\u0645 \u0646\u0636\u0639\u0647\u0645 \u062f\u0627\u062e\u0644 \u0627\u0644 \u064d\u062d\u0634\u0649 \u0639\u0646 \u0637\u0631\u064a\u0642 \u0627\u0644 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/innerHTML\" rel=\"noopener\" target=\"_blank\">innerHTML<\/a>\r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-javascript\">\r\n$textarea.oninput = function () {\r\n  $count.innerHTML = $maxLength - this.value.length;\r\n};\r\n<\/script>\r\n<div class=\"text\">\r\n\u0627\u0644\u0622\u0646 \u0627\u0644\u0643\u0648\u062f \u064a\u0639\u0645\u0644 \u0628\u0646\u062c\u0627\u062d \u0648\u0628\u062f\u0648\u0646 \u0623\u064a \u0645\u0634\u0643\u0644\u0629 \u0648\u0628\u0642\u0649 \u0634\u064a\u0621 \u0628\u0633\u064a\u0637 \u062c\u062f\u0627 \u0648\u0647\u0648 \u0639\u0646\u062f \u0627\u0644\u0648\u0635\u0648\u0644 \u0644\u0631\u0642\u0645 0 \u0646\u0642\u0648\u0645 \u0628\u0648\u0636\u0639 Class \u0627\u0633\u0645\u0647 zero \u0639\u0644\u0649 \u0627\u0644 Span \u0644\u064a\u0643\u0648\u0646 \u0644\u0648\u0646\u0647\u0627 \u0627\u062d\u0645\u0631 \u0648\u0627\u0630\u0627 \u0643\u0627\u0646 \u0627\u0644\u0631\u0642\u0645 \u0623\u0643\u0628\u0631 \u0645\u0646 0 \u0646\u0642\u0648\u0645 \u0628\u0625\u0632\u0627\u0644\u0629 \u0647\u0630\u0627 \u0627\u0644 Class \u0639\u0646 \u0637\u0631\u064a \u062e\u0627\u0635\u064a\u0629 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/classList\" rel=\"noopener\" target=\"_blank\">classList<\/a>\r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-javascript\">\r\n$textarea.oninput = function () {\r\n  $count.innerHTML = $maxLength - this.value.length;\r\n  if ($count.innerHTML == 0) {\r\n    $count.classList.add('zero');\r\n  } else {\r\n    $count.classList.remove('zero');\r\n  }\r\n};\r\n<\/script>\r\n<div class=\"text\">\r\n\u0648\u0627\u0644\u0622\u0646 \u0627\u0644\u0643\u0648\u062f \u0627\u0644\u0643\u0627\u0645\u0644 \u0643\u0627\u0644\u062a\u0627\u0644\u064a\r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-javascript\">\r\nvar $count = document.getElementById('the-count'),\r\n    $textarea = document.getElementById('the-text'),\r\n    $maxLength = $textarea.getAttribute('maxlength');\r\n$textarea.oninput = function () {\r\n  $count.innerHTML = $maxLength - this.value.length;\r\n  if ($count.innerHTML == 0) {\r\n    $count.classList.add('zero-count');\r\n  } else {\r\n    $count.classList.remove('zero-count');\r\n  }\r\n};\r\n<\/script>\r\n<div class=\"text\">\r\n\u0648\u0627\u0644\u0622\u0646 \u0643\u0644 \u0645\u0627 \u0639\u0644\u064a\u0643 \u0647\u0648 \u062a\u0646\u0633\u064a\u0642 \u0627\u0644 Class \u0627\u0644\u0645\u0633\u0645\u0649 zero \u0628\u0623\u064a \u0644\u0648\u0646 \u064a\u0639\u062c\u0628\u0643 \r\n<\/div>\r\n<script type=\"text\/plain\" class=\"language-css\">\r\n.zero {\r\n  color: red;\r\n  font-weight: bold;\r\n}\r\n<\/script>\r\n<div class=\"text\">\r\n\u0648\u0644\u062a\u062c\u0631\u0628\u0629 \u0627\u0644\u0643\u0648\u062f \u0643\u0627\u0645\u0644\u0627 \u0647\u0646\u0627\r\n<\/div>\r\n<div class=\"tutorial-demo\">\r\n<form class=\"textarea-count\">\r\n  <textarea class=\"form-control\" id=\"the-text\" rows=\"8\" cols=\"80\" maxlength=\"100\"><\/textarea>\r\n  <span class=\"textarea-counter\" id=\"the-count\">100<\/span>\r\n<\/form>\r\n<style>\r\n  .textarea-count {\r\n    position: relative;\r\n  }\r\n  .textarea-count textarea {\r\n    padding: 15px;\r\n    resize: none;\r\n    background-color: #fcfcfc;\r\n  }\r\n  .textarea-count textarea:focus {\r\n    box-shadow: none;\r\n  }\r\n  .textarea-count .textarea-counter {\r\n    position: absolute;\r\n    left: 8px;\r\n    bottom: 8px;\r\n    padding: 2px 10px 0;\r\n    background-color: #f8f8f8;\r\n    font-weight: bold;\r\n    border-radius: 6px;\r\n  }\r\n  .textarea-count .zero-count {\r\n    color: #F00;\r\n  }\r\n<\/style>\r\n<script>\r\nvar $count = document.getElementById('the-count'),\r\n    $textarea = document.getElementById('the-text'),\r\n    $maxLength = $textarea.getAttribute('maxlength');\r\n$textarea.oninput = function () {\r\n  $count.innerHTML = $maxLength - this.value.length;\r\n  if ($count.innerHTML == 0) {\r\n    $count.classList.add('zero-count');\r\n  } else {\r\n    $count.classList.remove('zero-count');\r\n  }\r\n};\r\n<\/script>\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u0643\u064a\u0641\u064a\u0629 \u062d\u0633\u0627\u0628 \u0639\u062f\u062f \u0627\u0644\u0623\u062d\u0631\u0641 Characters \u062f\u0627\u062e\u0644 \u0639\u0646\u0635\u0631 \u0627\u0644 Textarea \u0639\u0646\u062f \u0627\u0644\u0643\u062a\u0627\u0628\u0629 \u0648\u0639\u0646\u062f \u0627\u0644\u0625\u062f\u062e\u0627\u0644<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[525],"tags":[530,531,532,533,92],"class_list":["post-1165","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorials","tag-calculate","tag-characters","tag-textarea","tag-533","tag-92"],"_links":{"self":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/1165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/comments?post=1165"}],"version-history":[{"count":11,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/1165\/revisions"}],"predecessor-version":[{"id":8184,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/1165\/revisions\/8184"}],"wp:attachment":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/media?parent=1165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/categories?post=1165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/tags?post=1165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}