LoginSignup
0
1

More than 5 years have passed since last update.

jqueryで入力文字数を取得して、画像を切り替える方法

Last updated at Posted at 2017-10-05

keyupを使用して入力文字を検知

$('inputを指定する').keyup(function() {
    var length = $("inputを指定する").val().length
});

入力文字を比較する。

if(2 <= length) {
    // 実行した処理を記載する。
}

表示を切り替える

// display:noneをblockに変更する
$("display:noneをblockをしたい箇所を指定").show();
// display:blockをnoneに変更する
$("display:blockをnoneをしたい箇所を指定").hide();

上記の部分を合わせると入力文字数により表示を切り替えれる。

$('inputを指定する').keyup(function() {
    var length = $("inputを指定する").val().length
    if(2 <= length) {
        $("display:noneをblockをしたい箇所を指定").show();
        $("display:blockをnoneをしたい箇所を指定").hide();
    }
    if(1 >= length) {
        $("display:noneをblockをしたい箇所を指定").show();
        $("display:blockをnoneをしたい箇所を指定").hide();
    }
});
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1