아

[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-11 조회 3,187
12-02-11 조회 3,076
12-02-11 조회 3,513
12-02-10 조회 1만
12-02-10 조회 3,670
12-02-10 조회 4,634
12-02-09 조회 4,003
12-02-09 조회 3,562
12-02-09 조회 6,500
12-02-09 조회 3,214
12-02-08 조회 3,657
12-02-08 조회 3,058
12-02-08 조회 3,118
12-02-08 조회 4,067
12-02-08 조회 4,778
12-02-08 조회 3,113
12-02-07 조회 3,441
12-02-07 조회 3,375
12-02-07 조회 3,383
12-02-07 조회 2,688
12-02-07 조회 3,431
12-02-07 조회 2,977
12-02-06 조회 3,156
12-02-06 조회 3,049
12-02-06 조회 3,611
12-02-06 조회 4,050
12-02-06 조회 3,396
12-02-06 조회 1.2만
12-02-06 조회 4,327
12-02-06 조회 5,772
12-02-05 조회 2,856
12-02-05 조회 3,633
12-02-05 조회 3,037
12-02-04 조회 2,957
12-02-04 조회 2,951
12-02-04 조회 6,781
12-02-03 조회 3,956
12-02-03 조회 4,346
12-02-03 조회 2,849
12-02-03 조회 3,390
12-02-03 조회 3,978
12-02-02 조회 3,014
12-02-02 조회 3,787
12-02-02 조회 3,819
12-02-02 조회 5,486
12-02-02 조회 3,441
12-02-02 조회 4,703
12-02-02 조회 3,553
12-02-01 조회 3,385
12-02-01 조회 3,627
12-02-01 조회 4,055
12-02-01 조회 3,344
12-02-01 조회 2,891
12-01-31 조회 3,315
12-01-31 조회 3,564
12-01-31 조회 2,967
12-01-30 조회 9,737
12-01-30 조회 8,905
12-01-30 조회 2,881
12-01-30 조회 3,296
12-01-30 조회 3,385
12-01-30 조회 2,894
12-01-30 조회 3,061
12-01-29 조회 3,385
12-01-29 조회 3,250
12-01-29 조회 4,713
12-01-29 조회 2,893
12-01-29 조회 3,603
12-01-29 조회 3,894
12-01-28 조회 3,516
12-01-28 조회 2,749
12-01-28 조회 3,564
12-01-27 조회 2,953
12-01-27 조회 2,866
12-01-27 조회 2,911
12-01-27 조회 3,358
12-01-26 조회 6,651
12-01-26 조회 3,713
12-01-26 조회 3,496
12-01-26 조회 3,029
12-01-26 조회 3,125
12-01-25 조회 3,200
12-01-25 조회 3,005
12-01-25 조회 2,873
12-01-25 조회 2,681
12-01-25 조회 2,549
12-01-25 조회 4,282
12-01-25 조회 4,150
12-01-25 조회 2,757
12-01-25 조회 4,114
12-01-25 조회 3,590
12-01-24 조회 3,304
12-01-24 조회 3,508
12-01-24 조회 3,724
12-01-24 조회 4,435
12-01-22 조회 3,521
12-01-22 조회 2,866
12-01-22 조회 3,276
12-01-22 조회 3,773
12-01-21 조회 3,123