the objective is to redirect the user according to the option chosen in the drop-down menu. ex. if user chose apple redirect user to apple.html page when he hit SUBMIT button.
I know it’s basic but don’t curse me my mother =]
all the best.
<body>
<form name="myform" id="myForm">
<select>
<option id="apple">Apple</option>
<option id="orange">Orange</option>
<option id="pineapple">Pineapple</option>
<option id="banana" selected>Banana</option>
</select>
<p>
<input type="submit" name="submit" value="save">
</p>
</form>
<script type="text/javascript">
function OnSubmitForm(e){
e.preventDefault()
console.log(e.target)
if(document.getElementById("apple").selected ==true)
{
location.href ="apple.html";
}
else if(document.getElementById("pineapple").selected ==true)
{
location.href ="pineapple.html";
}
else if(document.getElementById("orange").selected ==true)
{
location.href ="orange.html";
}
else if(document.getElementById("banana").selected ==true)
{
location.href ="banana.html";
}
}
document.getElementById("myForm").addEventListener("submit", function(e) {
e.preventDefault();
if(document.getElementById("apple").selected ==true)
{
location.href ="apple.html";
}
else if(document.getElementById("pineapple").selected ==true)
{
location.href ="pineapple.html";
}
else if(document.getElementById("orange").selected ==true)
{
location.href ="orange.html";
}
else (document.getElementById("banana").selected ==true)
{
location.href ="banana.html";
}
});
</script>
</body>
I already spent hours researching but my IQ is 60. Anyone who can help has my eternal gratitude.
2
Answers
This should work:
Explanation:
value
attribute on each<option>
to store a unique valuedocument.getElementById('fruit').value
window.location.href
The option elements should have a value attribute and the select element should have a name attribute.
In the submit callback function
e.target
refer to the form and then you can get the select element by it’s name and the value will be the value of the selected option.