Firstly, I'm very new to Javascript, so I'm sorry if this is an easy question. My goal is to display the result of my switch statement in the tag. For now, I'm just trying to console.log it, but I can't seem to get it to work. My difficulty seems to be in storing the value from the drop down list in a variable that I can use in my switch statement. Here is what I've tried:
var behaviour = document.getElementById("behaviour");
var kentsBehaviour = behaviour.value;
console.log(kentsBehaviour)
//var h3 = document.getElementsByTagName("h3");
function kentsReward (kentsBehaviour) {
var prize;
switch (kentsBehaviour) {
case "Really good":
prize = "You get foot rubs!";
break;
case "Good":
prize = "You get an Oreo";
break;
case "Medium":
prize = "You get bread";
break;
case "Not so good":
prize = "You don't get anything";
break;
case "Greer":
prize = "You get pinches";
break;
default:
return "Tell me how you behaved today";
}
console.log( prize);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Kent's Rewards</title>
</head>
<body>
<h1>Please select your behaviour for today:</h1>
<select name="behaviour" id="behaviour" onChange="kentsReward()">
<option value="default">Select your behaviour</option>
<option value="Really good">Really good</option>
<option value="Good">Good</option>
<option value="Medium">Medium</option>
<option value="Not so good">Not so good</option>
<option value="Bad">Bad</option>
<option value="Greer">Greer</option>
</select>
<h2>Your reward is:</h2>
<h3></h3>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
HTMLto this:onChange="kentsReward(this.value)", inline event handlers are discouraged now a days, you should switch to usingdocument.getElementById("behaviour").addEventListener("change", function(){ kentsReward(this.value); });thisrefers to the drop down list inside of theevent handler functionso you then pass it'svalueto yourfunction. You can remove the first 3 lines of code.getElementById("tagId").innerHtml = kentsReward(kentsBehaviour).