ログイン
問題5

inputの値を取得して誕生日を祝おう

アイコン画像

問題5:inputの値を取得して誕生日を祝おう

inputタグに入力した値を取得して、
自分の誕生日なら「おめでとう!」と表示させよう。

※bootstrapを読み込むと
見本通りのレイアウトが綺麗になります。

エディターのアイコンあらかじめエディタに書くコード

                <div class="input-group mb-3">
    <h1>誕生日おめでとう!</h1>  
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon1">@</span>
  </div>
  <input type="date" id="birthday" class="form-control" value="1990-01-01" placeholder="誕生日">
</div>
¥HTML¥
              
                h1{
    text-align: center;
    color: red;
    font-size: 50px;
    width: 100%;
    font-weight: bold;
    color: #458fa0;
    display: none;
}
.input-group{
    width: 500px;
    margin: 300px auto 0 auto;
}
¥CSS¥
              

ブラウザのアイコン期待する画面


タグアイコン解答ソースコード

              $(function(){
    const birthday = '1990-01-25'; //ここはご自分の誕生日にする
    $('#birthday').on("change", function(){
      if( $(this).val() == birthday){
        $('h1').fadeIn();
      }
    });
  });
¥JavaScript¥
            
              <!DOCTYPE html>
<html lang="">
<head>
	<meta charset="UTF-8">
	<title>タイトル</title>
	<link href="https://fonts.googleapis.com/css?family=Kosugi+Maru&display=swap" rel="stylesheet">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<meta name="viewport" content="width=device-width">
	<style>
		h1{
			text-align: center;
			color: red;
			font-size: 50px;
			width: 100%;
			font-weight: bold;
			color: #458fa0;
			display: none;
		}
		.input-group{
			width: 500px;
			margin: 300px auto 0 auto;
		}
	</style>
</head>
<body>
	
	<div class="input-group mb-3">
		<h1>誕生日おめでとう!</h1>	
	  <div class="input-group-prepend">
	    <span class="input-group-text" id="basic-addon1">@</span>
	  </div>
	  <input type="date" id="birthday" class="form-control" value="1990-01-01" placeholder="誕生日">
	</div>
	
	
	<script
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
</body>
</html>
¥HTML¥
            

コメントのアイコン解説

$('#birthday').on("change", function(){ 〜〜
とすることでinputの値に変更があった場合の検知ができます。
検知ができら「$(this).val()」で値を取得して
判別しましょう。
ログインして解答を見る
完了にする!
twitterのアイコン
活動記録をTweetする
1.JavaScriptでできること
続きの動画を見たい方は公式LINEから「JavaScript」と送信すると動画が見れます。
JavaScriptを学ぶなら現役エンジニア監修「甲賀コース」
LINEの友達追加でお役立ち動画をGET!!

閉じる