I have a drop down field on my form. When I select a value from the drop down it immediately resets the focus to the top of the form.
To be clearer I have a drop down menu at the top of the screen and several input fields. The user would have to scroll down to the actual drop down field in order to select it. Once they select the value the page scrolls back to the top.
How can I keep the position the user selected on the form once a user selects a value from the drop down? [NOTE: It will not show up on stack overflow you would have to copy and paste the code and try it on your own browser].
I know it has to do with the code inside the HTML.
document.getElementById("locale").options[value].selected;
window.location.hash="country-select";
Because I keep getting an error: Uncaught TypeError: Cannot read property 'selected' of undefined
So far I have
// creates the page dynamically
function GetSelectedItem(){
var option = document.getElementById("locale").value;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyWebsite</title>
<link rel="stylesheet" href="languageswitcher.css">
<script>
document.getElementById("locale").addEventListener('change', function(event) {
var selected = event.target.options[ event.target.selectedIndex ].value
console.log(selected);
window.location.hash = selected;
});
</script>
</head>
<body>
<header>
<div id="country-select">
<form action="" method = "get">
<select id= "locale" name="locale">
<option value="en_US">English(US)</option>
<option value="en_GB">English(UK)</option>
<option value="bg_BG">Bulgarian</option>
<option value="cs_CS">Czech</option>
<option value="da_DK">Danish</option>
<option value="de_DE">German</option>
<option value="ek_GR">Greek</option>
<option value="es_ES">Spanish</option>
<option value="et_ET">Estonian</option>
<option value="fi_FI">Finnish</option>
<option value="fr_FR">French</option>
<option value="hu_HU">Hungarian</option>
<option value="it_IT">Italian</option>
<option value="lt_LT">Lithuanian</option>
<option value="lv_LV">Latvian</option>
<option value="nl_NL">Dutch</option>
<option value="no_NO">Norwegian</option>
<option value="pl_PL">Polish</option>
<option value="pt_PT">Portugese</option>
<option value="ro_RO">Romanian</option>
<option value="sk_SK">Slovak</option>
<option value="sl_SL">Slovenian</option>
<option value="sv_SE">Swedish</option>
</select>
<input value="Select" type="submit"/>
</form>
</div>
</header>
<script src="jquery_1.5.min.js"></script>
<script src="languageswitcher.js"></script>
</body>
</html>
Thanks for your help everyone, I am just trying to self learn and so far it's going great but I've been stuck on this for around 2 weeks and I have no idea why that .selected won't work at all. Also if there is any other efficient way to do this code such as using arrays that would be nice to know as well. Thanks again