おみくじ

あなたの本日の運勢は…?


<div id="omikuji">
<h1>おみくじ</h1>
<p id="output">あなたの本日の運勢は…?</p>
<input type="button" id="btn" value="占う">
</div>


window.onload = function(){
	var luck = ["大凶","凶","末吉","小吉","中吉", "吉", "大吉"];
	var colors = ["赤","青","黄","緑","白","黒","金","銀","茶","紫"];
	var message, random_luck, random_color;
	var btn = document.getElementById("btn"); // id="btn"を取得
	var output = document.getElementById("output"); // id="output"を取得

btn.onclick = function(){
	random_luck = luck[ Math.floor( Math.random() * luck.length ) ]; // luckの[i]番目の要素
	random_color = colors[ Math.floor( Math.random() * colors.length ) ]; // colorsの[i]番目の要素
	message = "<h2>" + random_luck + "</h2>";
	message += "<p>ラッキーカラーは " + random_color + "</p>";
	output.innerHTML = message;
} // End btn.onclick

} // End JavaScript