아

[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-02-26 조회 3,179
12-02-26 조회 3,496
12-02-26 조회 3,341
12-02-26 조회 3,420
12-02-26 조회 3,465
12-02-26 조회 3,320
12-02-26 조회 3,643
12-02-26 조회 3,477
12-02-26 조회 3,559
12-02-26 조회 4,041
12-02-26 조회 2,991
12-02-26 조회 5,184
12-02-25 조회 2,751
12-02-25 조회 3,230
12-02-24 조회 2,932
12-02-24 조회 3,313
12-02-24 조회 3,291
12-02-24 조회 3,607
12-02-24 조회 3,139
12-02-24 조회 3,944
12-02-24 조회 3,915
12-02-24 조회 3,512
12-02-24 조회 9,369
12-02-23 조회 2,876
12-02-23 조회 3,322
12-02-23 조회 3,224
12-02-23 조회 3,165
12-02-23 조회 3,811
12-02-23 조회 2,896
12-02-23 조회 3,375
12-02-23 조회 3,054
12-02-23 조회 2,810
12-02-22 조회 3,710
12-02-22 조회 5,472
12-02-22 조회 3,027
12-02-22 조회 4,425
12-02-22 조회 3,296
12-02-22 조회 3,119
12-02-22 조회 4,033
12-02-22 조회 3,917
12-02-22 조회 2,845
12-02-22 조회 3,887
12-02-22 조회 4,346
12-02-22 조회 3,769
12-02-22 조회 2,949
12-02-22 조회 4,599
12-02-21 조회 3,162
12-02-21 조회 2,942
12-02-21 조회 2,847
12-02-21 조회 3,756
12-02-21 조회 3,995
12-02-21 조회 3,417
12-02-21 조회 2,930
12-02-20 조회 3,174
12-02-20 조회 3,808
12-02-20 조회 3,757
12-02-20 조회 3,039
12-02-20 조회 5,821
12-02-20 조회 3,618
12-02-20 조회 6,832
12-02-20 조회 3,229
12-02-20 조회 4,204
12-02-19 조회 3,387
12-02-18 조회 3,122
12-02-18 조회 3,864
12-02-17 조회 2,999
12-02-17 조회 4,188
12-02-17 조회 3,071
12-02-17 조회 3,633
12-02-17 조회 3,405
12-02-17 조회 6,273
12-02-17 조회 4,466
12-02-17 조회 3,093
12-02-17 조회 3,122
12-02-17 조회 3,482
12-02-16 조회 3,309
12-02-16 조회 3,380
12-02-16 조회 4,745
12-02-16 조회 2,995
12-02-16 조회 5,382
12-02-16 조회 5,129
12-02-15 조회 3,675
12-02-15 조회 3,295
12-02-15 조회 4,834
12-02-15 조회 3,327
12-02-15 조회 3,720
12-02-15 조회 2,795
12-02-15 조회 4,067
12-02-14 조회 3,584
12-02-14 조회 3,690
12-02-14 조회 4,098
12-02-14 조회 3,138
12-02-14 조회 6,219
12-02-14 조회 3,170
12-02-14 조회 4,193
12-02-13 조회 3,469
12-02-13 조회 3,088
12-02-13 조회 7,236
12-02-12 조회 2,909
12-02-12 조회 5,287