As it says in the question, I’m trying to take an element from a dropdown menu and relate it to a stored variable and then use an if statement to put the variable in another location on a web page. The example I have below has a dropdown with months and I want to post the number of days using getElementByID. What is a good way to make this work? Any help would be appreciated. Here is my html/JavaScript excluding CSS better or worse-
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script language="javascript">
function displayDaysInTheMonth()
{
var thirty = document.monthForm.30
var thirtyone = document.monthForm.31
var twentyeight = document.monthForm.28
if (thirty == 'true' )
{
document.monthForm.getElementById('a').innerHTML = "has 30 days";
return;
}
else if (thirtyone == 'true' )
{
document.monthForm.getElementById('b').innerHTML = "has 31 days";
return;
}
else if (twentyeight == 'true' )
{
document.monthForm.getElementById('c').innerHTML = "has 28 days";
return;
}
}
</script>
</head>
<body>
<div class='container'>
<h2> David Platt's</h2>
<h2>Monthly Day Count</h2>
<form name='monthForm'>
<select name='months' onchange='displayDaysInTheMonth();' style='float: left; margin: 0 10px 10px 15px;'>
<option name = '31'>January</option>
<option name = '28'>February</option>
<option name = '31'>March</option>
<option name = '30'>April</option>
<option name = '31'>May</option>
<option name = '30'>June</option>
<option name = '31'>July</option>
<option name = '31'>August</option>
<option name = '30'>September</option>
<option name = '31'>October</option>
<option name = '30' >November</option>
<option name = '31'>December </option>
</select>
<div name='result' class='result' id = 'a' id = 'b' id = 'c' >has # days.</div>
<div class='clear'></div>
</form>
</div>
</body>
</html>
You can use jquery API to do this:
To Get dropdown value and set Div value to selected value do this
Change your div to
Change Drop down To