아

[jQuery] textarea 글자수 카운트

트위터처럼 140자 제한이 있는 경우, 남은 글자수를 보여줍니다.

0보다 아래로 떨어지면 붉은 글씨로 바뀌고 submit을 할 수 없게 했습니다.


index.html

<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
        <title>Input Counter</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <link href="stylesheet.css" media="screen" rel="stylesheet" type="text/css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
        <script src="application.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="container">
            <form action="index.html" method="POST">
                <textarea></textarea>
                <span class="input-counter"></span>
                <input type="submit" id="input-submit" value="등록" />
            </form>
        </div>
    </body>
</html>


stylesheet.js

body {
    font-size: 12px;
    background: #FFF;
    color: #333;
    margin: 0;
}

#container {
    margin: 10px auto;
    width: 500px;
    padding: 10px;
    background: #CCC;
}

form {
    overflow: hidden;
    height: 100%;
}

textarea {
    display: block;
    width: 99%;
    height: 5.0em;
    margin-bottom: 0.5em;
}

span.input-counter {
    float: left;
    font-weight: bold;
    color: #000;
}

span.input-counter.disabled {
    color: #F00;
}

input#input-submit {
    float: right;
}


application.js

$(function() {
    var maxLength = 140;

    function updateInputCount() {
        var textLength = $('textarea').val().length;
        var count = maxLength - textLength;
        $('span.input-counter').text(count);
        if (count < 0) {
            $('span.input-counter').addClass('disabled');
            $('input#input-submit').prop('disabled', true);
        } else {
            $('span.input-counter').removeClass('disabled');
            $('input#input-submit').prop('disabled', false);
        }
    }

    $('textarea')
        .focus(updateInputCount)
        .blur(updateInputCount)
        .keypress(updateInputCount);
    window.setInterval(updateInputCount, 100);

    updateInputCount();
});




댓글 5개

jQuery 1.7이 나왔는데 Google Libraries API 쪽에선 아직 제공하지 않는 것 같네요.
제이쿼리 1.86 도 나왔는데요 ....
jQuery와 jQuery UI는 서로 다르며, 버전 체계가 완전히 다릅니다.

둘의 관계는 예전에 많이 쓰였던 Prototype과 script.aculo.us의 관계와 유사하죠.

jQuery 1.7은 2011년 11월 3일에 나왔고,

jQuery UI 1.8.16은 2011년 6월 30일에 나왔습니다.
감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
11-08-06 조회 2,968
11-08-06 조회 2,647
11-08-06 조회 3,356
11-08-05 조회 3,024
11-08-05 조회 2,886
11-08-05 조회 3,225
11-08-05 조회 3,163
11-08-05 조회 2,637
11-08-05 조회 2,546
11-08-05 조회 2,334
11-08-05 조회 2,535
11-08-05 조회 3,385
11-08-05 조회 4,018
11-08-04 조회 4,059
11-08-04 조회 3,585
11-08-04 조회 3,071
11-08-04 조회 3,087
11-08-04 조회 2,943
11-08-04 조회 3,201
11-08-04 조회 2,187
11-08-04 조회 3,076
11-08-04 조회 2,222
11-08-04 조회 2,721
11-08-04 조회 2,497
11-08-04 조회 3,696
11-08-03 조회 2,414
11-08-03 조회 4,213
11-08-03 조회 2,747
11-08-03 조회 2,540
11-08-03 조회 4,614
11-08-03 조회 2,247
11-08-03 조회 2,619
11-08-03 조회 2,070
11-08-02 조회 2,585
11-08-02 조회 2,158
11-08-02 조회 2,917
11-08-02 조회 2,259
11-08-02 조회 2,768
11-08-02 조회 2,312
11-08-02 조회 2,223
11-08-01 조회 2,140
11-08-01 조회 3,046
11-08-01 조회 2,356
11-08-01 조회 2,208
11-08-01 조회 2,232
11-07-30 조회 2,968
11-07-30 조회 2,936
11-07-29 조회 2,126
11-07-29 조회 4,071
11-07-28 조회 2,011
11-07-28 조회 2,240
11-07-28 조회 4,834
11-07-27 조회 2,279
11-07-27 조회 2,711
11-07-27 조회 3,002
11-07-26 조회 2,142
11-07-26 조회 2,264
11-07-26 조회 2,328
11-07-26 조회 2,672
11-07-26 조회 2,252
11-07-26 조회 2,561
11-07-26 조회 4,506
11-07-26 조회 2,903
11-07-26 조회 2,317
11-07-26 조회 3,126
11-07-25 조회 2,417
11-07-25 조회 2,003
11-07-25 조회 3,899
11-07-25 조회 2,303
11-07-25 조회 2,408
11-07-23 조회 2,643
11-07-23 조회 2,589
11-07-23 조회 2,745
11-07-22 조회 2,153
11-07-22 조회 4,085
11-07-22 조회 2,421
11-07-21 조회 3,063
11-07-21 조회 2,598
11-07-21 조회 2,703
11-07-21 조회 2,364
11-07-20 조회 2,602
11-07-20 조회 2,170
11-07-20 조회 2,224
11-07-20 조회 2,977
11-07-20 조회 2,801
11-07-20 조회 2,591
11-07-20 조회 2,642
11-07-20 조회 3,544
11-07-20 조회 3,227
11-07-19 조회 2,163
11-07-19 조회 2,992
11-07-19 조회 2,862
11-07-19 조회 2,871
11-07-18 조회 2,399
11-07-18 조회 2,387
11-07-18 조회 2,354
11-07-18 조회 2,347
11-07-18 조회 3,024
11-07-18 조회 2,395
11-07-17 조회 2,639