아

[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-21 조회 3,369
12-01-21 조회 4,160
12-01-21 조회 3,562
12-01-21 조회 3,234
12-01-20 조회 3,125
12-01-20 조회 2,765
12-01-20 조회 3,523
12-01-20 조회 3,356
12-01-20 조회 2,743
12-01-20 조회 3,185
12-01-20 조회 6,274
12-01-19 조회 3,302
12-01-19 조회 4,958
12-01-19 조회 3,234
12-01-19 조회 3,239
12-01-19 조회 3,200
12-01-19 조회 2,820
12-01-18 조회 3,761
12-01-18 조회 2,973
12-01-18 조회 2,714
12-01-18 조회 3,287
12-01-18 조회 2,944
12-01-18 조회 3,132
12-01-17 조회 2,924
12-01-17 조회 4,016
12-01-17 조회 4,448
12-01-17 조회 3,305
12-01-17 조회 3,417
12-01-17 조회 3,172
12-01-17 조회 5,243
12-01-17 조회 3,037
12-01-16 조회 2,775
12-01-16 조회 3,626
12-01-16 조회 4,116
12-01-15 조회 2,921
12-01-15 조회 3,739
12-01-15 조회 3,697
12-01-14 조회 3,587
12-01-14 조회 3,733
12-01-14 조회 3,465
12-01-14 조회 1.1만
12-01-14 조회 3,677
12-01-14 조회 3,997
12-01-14 조회 2,951
12-01-14 조회 2,794
12-01-13 조회 2,940
12-01-13 조회 3,523
12-01-13 조회 4,589
12-01-13 조회 2,932
12-01-12 조회 4,245
12-01-12 조회 4,383
12-01-12 조회 3,393
12-01-12 조회 3,216
12-01-11 조회 2,806
12-01-11 조회 3,252
12-01-11 조회 3,465
12-01-11 조회 3,186
12-01-11 조회 3,193
12-01-11 조회 3,300
12-01-11 조회 3,509
12-01-11 조회 4,291
12-01-11 조회 3,374
12-01-11 조회 2,833
12-01-11 조회 1.2만
12-01-11 조회 3,161
12-01-10 조회 2,968
12-01-10 조회 3,684
12-01-10 조회 4,748
12-01-10 조회 3,440
12-01-10 조회 3,153
12-01-10 조회 2,894
12-01-10 조회 3,805
12-01-10 조회 3,069
12-01-10 조회 4,585
12-01-10 조회 2,962
12-01-10 조회 3,001
12-01-10 조회 3,194
12-01-10 조회 3,826
12-01-10 조회 2,894
12-01-10 조회 4,526
12-01-10 조회 2,910
12-01-10 조회 5,386
12-01-10 조회 5,568
12-01-10 조회 5,446
12-01-10 조회 5,344
12-01-09 조회 2,864
12-01-09 조회 2,789
12-01-09 조회 3,322
12-01-09 조회 3,847
12-01-09 조회 2,797
12-01-09 조회 2,960
12-01-09 조회 2,762
12-01-08 조회 7,102
12-01-08 조회 2,777
12-01-08 조회 7,190
12-01-08 조회 3,713
12-01-08 조회 3,310
12-01-08 조회 2,714
12-01-08 조회 2,866
12-01-08 조회 3,505