jQueryでテキストボックスの文字数をカウントする

jQueryでテキストボックスの文字数をカウントする

jQueryでテキストボックスの文字数をカウントしてみます。

bindメソッドを使ってkeyupとchange時に文字数をカウント(正確にはlength)したものを表示しています。

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>サンプル</title>
<script src="http://code.jquery.com/jquery.js"></script>
<!-- コーディング部分 -->
<script type="text/javascript">
$(function(){
  $("#text").bind("keyup change", function() {
    $("#count").text($(this).val().length);
  });
});
</script>
<!-- コーディング部分 -->
</head>
<body>
<input type="text" id="text" />
<p id="count"></p>
</body>
</html>

デモです。

コメント

タイトルとURLをコピーしました