크게 작게 인쇄

[그누보드]textarea 글자수 표기하기

charcount.js 

/*
If you want to use this script, please keep the original author in this header!

Purpose: Script for applying maxlengths to textareas and monitoring their character lengths.
Author: James O''Cull
Date: 08/14/08


사용법: textarea가 끝나는 부분에 script를 불러오시면 됩니다 ;)

<textarea maxlength="1000"></textarea>
<script type="text/javascript" language="javascript" src="<?="$g4[path]/js/charcounter.js"?>"></script>

If you add a new text area with javascript, simply call parseCharCounts() again find the new textarea(s) and label them!
*/

var LabelCounter = 0;

function parseCharCounts()
{
//Get Everything...
var elements = document.getElementsByTagName('textarea');
var element = null;
var maxlength = 9;
var newlabel = null;
for(var i=0; i < elements.length; i++)
{
element = elements[i];
if(element.getAttribute('maxlength') != null && element.getAttribute('limiterid') == null)
{
maxlength = element.getAttribute('maxlength');
//Create new label
newlabel = document.createElement('label');
newlabel.id = 'limitlbl_' + LabelCounter;
newlabel.style.color = 'red';
newlabel.style.display = 'block'; //Make it block so it sits nicely.
newlabel.innerHTML = "Updating...";
//Attach limiter to our textarea
element.setAttribute('limiterid', newlabel.id);
element.onkeyup = function(){ displayCharCounts(this); };
//Append element
element.parentNode.insertBefore(newlabel, element);
//Force the update now!
displayCharCounts(element);
}
//Push up the number
LabelCounter++;
}
}

function displayCharCounts(element)
{
var limitLabel = document.getElementById(element.getAttribute('limiterid'));
var maxlength = element.getAttribute('maxlength');
var enforceLength = false;
if(element.getAttribute('lengthcut') != null && element.getAttribute('lengthcut').toLowerCase() == 'true')
{
enforceLength = true;
}
//Replace \r\n with \n then replace \n with \r\n
//Can''t replace \n with \r\n directly because \r\n will be come \r\r\n

//We do this because different browsers and servers handle new lines differently.
//Internet Explorer and Opera say a new line is \r\n
//Firefox and Safari say a new line is just a \n
//ASP.NET seems to convert any plain \n characters to \r\n, which leads to counting issues
var value = element.value.replace(/\u000d\u000a/g,'\u000a').replace(/\u000a/g,'\u000d\u000a');
var currentLength = value.length;
var remaining = 0;
if(maxlength == null || limitLabel == null)
{
return false;
}
remaining = maxlength - currentLength;
if(remaining >= 100)
{
limitLabel.style.color = 'green';
limitLabel.innerHTML = remaining + ' 글자 남았습니다';
}
else if(remaining < 100 && remaining >20)
{
limitLabel.style.color = 'orange';
limitLabel.innerHTML = remaining + ' 글자 남았습니다';
}
else
{
limitLabel.style.color = 'red';
limitLabel.innerHTML = remaining + ' 글자 남았습니다';
}
}

//Go find our textareas with maxlengths and handle them when we load!
parseCharCounts();


<textarea maxlength="1000"></textarea> 
<script type="text/javascript" language="javascript" src="<?="$g4[path]/js/charcounter.js"?>"></script> 
요렇게요 ;)



※ 로그인 하시면 원본파일을 볼 수 있습니다. ※

0
코멘트 0
자동등록방지 코드
그누보드팁 | 전체게시물 160
안내

포인트안내닫기

  • 글읽기0
  • 글쓰기100
  • 댓글쓰기50
  • 다운로드-500
그누보드팁리스트
번호 제목 글쓴이 날짜 조회 추천
 공지  텍스트 슈퍼맨 웹스터디 12-17 - -
160 파일첨부 +1 링크 슈퍼맨 웹스터디 12-07 4929 0
159 텍스트 +3 슈퍼맨 웹스터디 12-07 4320 0
158 텍스트 +1 슈퍼맨 웹스터디 01-10 5451 0
157 텍스트 슈퍼맨 웹스터디 12-14 5069 0
156 텍스트 링크 슈퍼맨 웹스터디 12-06 7200 0
155 텍스트 링크 슈퍼맨 웹스터디 11-27 5265 0
154 텍스트 링크 슈퍼맨 웹스터디 09-27 6243 0
153 텍스트 슈퍼맨 웹스터디 09-06 4945 0
152 텍스트 +2 링크 슈퍼맨 웹스터디 08-15 5197 1
151 파일첨부 링크 슈퍼맨 웹스터디 07-19 4557 0
150 파일첨부 링크 슈퍼맨 웹스터디 07-12 6244 0
149 파일첨부 링크 슈퍼맨 웹스터디 07-02 6014 0
148 파일첨부 +2 링크 슈퍼맨 웹스터디 06-06 4789 0
147 텍스트 슈퍼맨 웹스터디 04-02 4809 0
146 파일첨부 링크 슈퍼맨 웹스터디 03-28 4966 0
145 파일첨부 링크 슈퍼맨 웹스터디 03-28 4962 0
144 파일첨부 +1 슈퍼맨 웹스터디 03-19 4809 0
143 파일첨부 슈퍼맨 웹스터디 03-13 4930 0
142 텍스트 +4 슈퍼맨 웹스터디 03-12 6968 0
141 텍스트 링크 슈퍼맨 웹스터디 02-15 5316 0