I am a novice at Web Design and JavaScript. I have searched on here a bit and have tried multiple solutions I thought would work and so far nothing. I am working on an assignment for school. I am trying to use JavaScript to display a div which contains a form. I have two different divs set to display: none in my CSS file. Based on the value of a drop down I want to display the correct form. I tried to input a script and tried the onchange call as well, nothing happens with either. I don't even see errors in developer mode.
window.onload = function() {
document.getElementById("choice").onchange = function() {
var selection = this.value;
if (selection == "helpRequest")
document.getElementById('divHelpRequest').style.display = 'block';
if (selection == "feedback")
document.getElementById('formDiv').style.display = 'block';
}
}
<form name="surveyChoice" method="post" id="choice">
<fieldset>
<legend>Which Form do you Require</legend>
<select size="1" name="choice" id="choice">
<option>Select your form</option>
<option value="feedback">General Feedback</option>
<option value="helpRequest" onchange="function();">Help Request</option>
</select>
</fieldset>
</form>
ids should always be unique, butid="choice"occurs on two different elements.console.log()will be your lifeblood throughout your dev career. Make sure to use it. On doing aconsole.log(selection)you'll find that this.value is returningundefined. The rest of your code obviously won't work because you don't have an iff forundefined. You need to specify the onchange event in the markup and call a function. See this post: stackoverflow.com/questions/12080098/…idname of your form. When you callthisin your code, you are referring to your form because it is the first element with thatidonDOM.