아

[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건
+
제목 글쓴이 날짜 조회
13-03-03 조회 6,297
13-03-01 조회 5,623
13-03-01 조회 8,368
13-03-01 조회 6,378
13-02-26 조회 3,985
13-02-26 조회 3,934
13-02-26 조회 4,824
13-02-24 조회 8,206
13-02-24 조회 4,307
13-02-24 조회 3,550
13-02-24 조회 5,359
13-02-24 조회 5,345
13-02-24 조회 4,413
13-02-24 조회 5,743
13-02-24 조회 9,440
13-02-14 조회 5,189
13-02-13 조회 9,124
13-01-29 조회 7,740
13-01-24 조회 5,085
13-01-24 조회 4,966
13-01-17 조회 4,382
13-01-16 조회 4,425
13-01-08 조회 4,433
12-12-27 조회 4,136
12-12-20 조회 1.1만
12-12-20 조회 3,432
12-12-06 조회 4,456
12-12-01 조회 4,208
12-11-28 조회 5,788
12-11-25 조회 5,488
12-10-20 조회 4,715
12-10-03 조회 4,888
12-09-26 조회 5,029
12-09-15 조회 8,470
12-09-14 조회 5,484
12-09-11 조회 4,008
12-09-10 조회 3,998
12-09-05 조회 8,895
12-08-29 조회 4,726
12-08-26 조회 3,846
12-08-23 조회 3,552
12-08-22 조회 3,626
12-08-22 조회 4,439
12-08-21 조회 3,574
12-08-20 조회 3,507
12-08-20 조회 9,313
12-08-15 조회 4,437
12-08-13 조회 3,347
12-08-11 조회 4,758
12-08-05 조회 4,629
12-08-03 조회 3,396
12-08-03 조회 4,095
12-08-02 조회 5,084
12-07-29 조회 4,194
12-07-26 조회 3,763
12-07-26 조회 3,622
12-07-22 조회 3,550
12-07-20 조회 3,373
12-07-19 조회 4,244
12-07-19 조회 3,512
12-07-16 조회 3,619
12-07-16 조회 4,534
12-07-15 조회 3,630
12-07-12 조회 7,433
12-07-12 조회 5,082
12-07-11 조회 3,129
12-07-09 조회 3,288
12-07-05 조회 3,895
12-07-04 조회 4,476
12-07-04 조회 3,895
12-07-04 조회 4,412
12-06-29 조회 3,874
12-06-27 조회 3,472
12-06-24 조회 3,938
12-06-22 조회 4,258
12-06-22 조회 8,669
12-06-22 조회 3,313
12-06-21 조회 3,332
12-06-17 조회 3,983
12-06-14 조회 4,077
12-06-14 조회 3,360
12-06-08 조회 4,491
12-06-06 조회 3,453
12-06-04 조회 4,230
12-05-26 조회 5,031
12-05-26 조회 3,981
12-05-25 조회 3,271
12-05-24 조회 3,373
12-05-22 조회 3,430
12-05-20 조회 3,074
12-05-16 조회 3,180
12-05-15 조회 3,567
12-05-10 조회 3,992
12-05-08 조회 3,544
12-05-04 조회 3,378
12-05-03 조회 4,336
12-05-02 조회 3,471
12-05-02 조회 3,243
12-05-02 조회 3,341
12-04-26 조회 3,507