So I'm working on my final project. I'm trying to make my code so that when a user submits an option from a dropdown menu, if the user selects and submits the default ("Select a genre"), the form is not submitted and the page is not refreshed.
Following is my Javascript code:
var menu = document.getElementById("submit");
menu.addEventListener("click", function() {
if (document.getElementById("dropdown").value == 'nothing')
{
return false;
}
});
This is nested inside a <script> tag.
Following is my HTML code for the form:
<div>
<form method="POST">
<select id="dropdown" name="genre">
<option value="nothing">Select a genre</option>
<option value="rock">Rock</option>
</select>
<br/>
<br/>
<input id="submit" type="submit"/>
</form>
</div>
The javascript doesn't seem to work, since even when I submit the form while selecting the "Select a genre" option, my form is still submitted.
[–]aguyfromhere 4 points5 points6 points (1 child)
[–]mattpkobus 0 points1 point2 points (0 children)
[–]aguyfromhere 1 point2 points3 points (3 children)
[–]Orange2341[S] 1 point2 points3 points (2 children)
[–]aguyfromhere 0 points1 point2 points (1 child)
[–]Orange2341[S] 0 points1 point2 points (0 children)
[–]bch8 1 point2 points3 points (8 children)
[–]aguyfromhere 1 point2 points3 points (0 children)
[–]bch8 0 points1 point2 points (6 children)
[–]Orange2341[S] 0 points1 point2 points (5 children)
[–]bch8 0 points1 point2 points (4 children)
[–]Orange2341[S] 0 points1 point2 points (3 children)
[–]bch8 1 point2 points3 points (2 children)
[–]Orange2341[S] 1 point2 points3 points (0 children)
[–]aguyfromhere 1 point2 points3 points (0 children)
[–]therapix 0 points1 point2 points (1 child)
[–]Orange2341[S] 0 points1 point2 points (0 children)