1

I am trying to learn the onChange function. I grabbed this code and implemented it on my site and it is working, it actually shows the value of what the user has chosen but when I try to console log the variable in chrome it says: Uncaught ReferenceError: x is not defined. Why is that? Why is the variable not defined even after that I have chosen a car. And one more question. Is this javascript or Jquery?

The CODE

<!DOCTYPE html>
<html>
<body>

<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction()">
    <option value="Audi">Audi
    <option value="BMW">BMW
    <option value="Mercedes">Mercedes
    <option value="Volvo">Volvo
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

</body>
</html>
1
  • Not part of your question by your options have no close of the tag: <option value="Audi">Audi</option> Commented Feb 23, 2017 at 20:42

2 Answers 2

2

This is the JavaScript from your post.

If you do this:

function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
    console.log("x:",x);
}

x is in the scope of the function and thus is available.

It is generally a bad practice to define variables like that in the global scope.

var x = {};
function myFunction() {
    x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
 }
myfunction();// call it
console.log("x:",x);

Verbose version of this: (basically the same but explicit window on the objects) Note I added how to get the selected value.

window.x = {};

function myFunction() {
  window.console.log("in here");
  var selectElem = document.getElementById("mySelect");
  var optsCount = selectElem.options.length;
  var index = selectElem.selectedIndex;
  window.console.log("opts", optsCount, index);
  // return the value of the selected option
  window.console.log(selectElem.options[selectElem.selectedIndex].value)
  window.x = selectElem.options[selectElem.selectedIndex].value;
  document.getElementById("demo").innerHTML = "You selected: " + x;
}
window.myFunction(); // call it
window.console.log("x:", window.x);

Here is a fiddle of the last example:https://jsfiddle.net/MarkSchultheiss/bqwd784p/

No jQuery here just JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the describing answer. I learn a lot and made it work :-)
2

Your variable is declared and assigned in the function scope. If you want it available outside of the function scope you need to declare it outside of the function

<script>
var x;
function myFunction() {
    x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

It will be undefined until your function is triggered. This is plain vanilla JS using the DOM API.

3 Comments

I will try that. Thanks.
@KakiSami Optionally you can assign it globally from within the function with window.x = document.getElementById("mySelect").value ;
Thank you very much for the answer. I ended up declaring it on the fly and it then became global. "format = document.getElementById("format").value"

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.