아

[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,188건
+
제목 글쓴이 날짜 조회
11-09-19 조회 2,559
11-09-19 조회 2,481
11-09-19 조회 5,495
11-09-19 조회 6,143
11-09-16 조회 2,863
11-09-16 조회 3,117
11-09-16 조회 6,313
11-09-15 조회 3,085
11-09-15 조회 2,842
11-09-15 조회 2,515
11-09-15 조회 2,518
11-09-15 조회 2,711
11-09-15 조회 2,675
11-09-15 조회 2,793
11-09-14 조회 2,707
11-09-14 조회 3,272
11-09-14 조회 2,380
11-09-14 조회 2,303
11-09-14 조회 2,292
11-09-13 조회 2,475
11-09-10 조회 2,943
11-09-10 조회 2,566
11-09-09 조회 3,148
11-09-09 조회 3,005
11-09-09 조회 3,081
11-09-09 조회 3,126
11-09-08 조회 3,812
11-09-08 조회 5,009
11-09-08 조회 3,403
11-09-08 조회 4,337
11-09-08 조회 3,843
11-09-08 조회 3,640
11-09-08 조회 2,740
11-09-08 조회 2,304
11-09-08 조회 2,716
11-09-08 조회 2,826
11-09-07 조회 3,500
11-09-07 조회 3,561
11-09-07 조회 2,975
11-09-07 조회 2,686
11-09-07 조회 2,784
11-09-07 조회 2,803
11-09-07 조회 3,031
11-09-07 조회 2,301
11-09-07 조회 2,404
11-09-07 조회 2,561
11-09-07 조회 3,493
11-09-07 조회 2,775
11-09-06 조회 2,606
11-09-06 조회 2,883
11-09-06 조회 2,449
11-09-05 조회 2,663
11-09-05 조회 2,602
11-09-05 조회 2,886
11-09-05 조회 2,394
11-09-04 조회 2,364
11-09-04 조회 2,331
11-09-03 조회 2,505
11-09-03 조회 2,662
11-09-02 조회 3,395
11-09-02 조회 2,780
11-09-02 조회 2,641
11-09-02 조회 3,555
11-09-02 조회 2,583
11-09-02 조회 2,465
11-09-02 조회 2,465
11-09-01 조회 2,668
11-09-01 조회 2,810
11-09-01 조회 2,694
11-09-01 조회 2,692
11-09-01 조회 2,834
11-09-01 조회 2,686
11-09-01 조회 4,128
11-09-01 조회 3,729
11-09-01 조회 4,389
11-08-31 조회 2,264
11-08-31 조회 2,570
11-08-31 조회 2,861
11-08-31 조회 2,583
11-08-31 조회 2,429
11-08-31 조회 3,223
11-08-30 조회 2,979
11-08-30 조회 4,206
11-08-29 조회 2,977
11-08-29 조회 2,567
11-08-29 조회 5,177
11-08-29 조회 3,310
11-08-29 조회 2,717
11-08-29 조회 3,070
11-08-28 조회 2,469
11-08-28 조회 3,617
11-08-28 조회 2,568
11-08-28 조회 2,939
11-08-26 조회 2,472
11-08-26 조회 2,987
11-08-26 조회 3,221
11-08-26 조회 2,722
11-08-25 조회 2,556
11-08-25 조회 2,868
11-08-25 조회 2,591