아

[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건
+
제목 글쓴이 날짜 조회
12-01-08 조회 3,059
12-01-07 조회 3,664
12-01-07 조회 3,121
12-01-07 조회 3,029
12-01-07 조회 2,998
12-01-07 조회 5,252
12-01-06 조회 2,780
12-01-06 조회 3,494
12-01-06 조회 2,984
12-01-06 조회 2,938
12-01-06 조회 2,943
12-01-06 조회 5,649
12-01-06 조회 3,697
12-01-06 조회 2,892
12-01-06 조회 3,019
12-01-05 조회 3,080
12-01-05 조회 3,095
12-01-05 조회 2,805
12-01-05 조회 2,902
12-01-05 조회 3,549
12-01-05 조회 5,394
12-01-04 조회 3,839
12-01-04 조회 2,833
12-01-04 조회 2,777
12-01-04 조회 2,998
12-01-04 조회 2,838
12-01-04 조회 2,918
12-01-04 조회 3,075
12-01-04 조회 2,831
12-01-04 조회 5,241
12-01-04 조회 5,110
12-01-04 조회 2,969
12-01-04 조회 3,434
12-01-04 조회 3,108
12-01-04 조회 2,930
12-01-04 조회 5,427
12-01-04 조회 4,596
12-01-04 조회 4,081
12-01-04 조회 3,948
12-01-04 조회 2,774
12-01-03 조회 3,663
12-01-03 조회 2,643
12-01-03 조회 2,667
12-01-03 조회 2,670
12-01-02 조회 3,165
12-01-02 조회 3,222
12-01-02 조회 4,066
12-01-02 조회 3,035
12-01-02 조회 3,252
12-01-02 조회 3,516
12-01-02 조회 3,515
12-01-01 조회 2,716
12-01-01 조회 4,192
12-01-01 조회 3,162
12-01-01 조회 3,536
11-12-31 조회 3,473
11-12-31 조회 3,805
11-12-31 조회 3,906
11-12-31 조회 3,142
11-12-31 조회 3,293
11-12-31 조회 3,730
11-12-30 조회 3,986
11-12-30 조회 3,267
11-12-30 조회 3,323
11-12-30 조회 3,968
11-12-30 조회 5,153
11-12-30 조회 4,255
11-12-30 조회 4,426
11-12-30 조회 3,889
11-12-30 조회 3,609
11-12-30 조회 3,630
11-12-30 조회 3,475
11-12-30 조회 2,670
11-12-30 조회 3,837
11-12-30 조회 7,112
11-12-29 조회 3,683
11-12-29 조회 3,366
11-12-29 조회 3,031
11-12-29 조회 3,248
11-12-29 조회 3,104
11-12-28 조회 2,728
11-12-28 조회 2,875
11-12-28 조회 3,008
11-12-28 조회 2,932
11-12-28 조회 3,795
11-12-27 조회 3,152
11-12-27 조회 2,918
11-12-27 조회 2,822
11-12-27 조회 2,936
11-12-26 조회 3,258
11-12-26 조회 3,157
11-12-26 조회 3,734
11-12-26 조회 3,367
11-12-26 조회 5,924
11-12-25 조회 3,090
11-12-23 조회 3,642
11-12-23 조회 3,585
11-12-23 조회 5,160
11-12-23 조회 4,816
11-12-23 조회 2,904