0

So I have a simple HTML file that I want to have a select option drop down menu. I have made that when a selection is made the page reloads retaining the option but I can't get the result outside of the function.

<!doctype html>
<html>
<head>
</head><body>
  <select id="mySelect" onchange = "getSelectValue();">
    <optgroup label="Working">
        <option value="rage.zip">Streets of Rage</option>
        <option value="quest.zip">Money Quest</option>
    </optgroup>
</select>

<div style="width:640px;height:480px;max-width:100%">
<script type="text/javascript">
window.addEventListener('load', function(){
    if (localStorage.pick) {
        var sel = document.querySelector('#mySelect');
        sel.value = localStorage.pick;
    }
});

function getSelectValue(){
    var sel = document.querySelector('#mySelect');
    localStorage.pick = sel.value;
    location.reload();
    return sel.value;
}

    EJS_player = '#game';
    EJS_gameUrl = ; // Url to Game rom
    EJS_gameID = 4; // ID in your website, required for netplay.
    EJS_core = 'segaMD';
</script>
<script src="https://www.emulatorjs.com/loader.js"></script>
</body>
</html>

My issue is the select option result is in the sel.value. I need that value to EJS_gameUrl = .

So if the option 1 is select which is rage.zip then it should be filled in the EJS_gameUrl = "rage.zip"

Im just having trouble with getting that result out of the function . I would prefer to define it to a variable and just add the variable in the EJS_gameUrl = x; or similar. How can I go about this?

1
  • i believe i need to define the listener before the addEventListener. But Im unsure on how to proceed. Commented May 3, 2021 at 21:19

1 Answer 1

1

See Storing the information you need — Variables, Storage.setItem and Storage.getItem.

Your code should be something along the lines of:

const EJS_player = '#game';
let EJS_gameUrl = ''; // Url to Game rom
const EJS_gameID = 4; // ID in your website, required for netplay.
const EJS_core = 'segaMD';

window.addEventListener('load', function() {
  const storedValue = localStorage.getItem('mySelect');
  if (storedValue) {
    var sel = document.querySelector('#mySelect');
    sel.value = storedValue;
    EJS_gameUrl = storedValue;
  }
});

function getSelectValue() {
  localStorage.setItem('mySelect', this.value);
  location.reload();
}

console.log(EJS_gameUrl)
<select id="mySelect" onchange="getSelectValue();">
  <optgroup label="Working">
    <option value="rage.zip">Streets of Rage</option>
    <option value="quest.zip">Money Quest</option>
  </optgroup>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I'll need to read up on the variables. As going by your example doesn't seem to store it in the EJS_gameUrl. Will try to figure out what the console.log actually pulls if I try to alert(); on it it just gives me this "function log() { [native code] }"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.