1

I'm a beginner in JS. In my personal project I want to use a datalist of colors to change the div color.

I managed to do it with select. But it doesn't work with datalist. So I decided to console log the value of the variable declared in the function but I get the error: x is not defined. How to make it work? (It's probably a basic question)

const selectSection = document.querySelector(".select-section");

function myFunction() {
  var x = document.getElementById("my-select").value;
  selectSection.style.backgroundColor = x;
}

console.log(x);
<label for="my-select">Pick a color:</label>
<select name="" id="my-select" value="Choose the background color" onChange="myFunction()">
  <option value="default">default</option>
  <optgroup label="Warm Colors">
    <option value="red">red</option>
    <option value="orange">orange</option>
    <option value="yellow">yellow</option>
  </optgroup>
  <optgroup label="Cool Colors">
    <option value="green">green</option>
    <option value="blue">blue</option>
    <option value="purple">purple</option>
  </optgroup>
</select>

6
  • 1
    Declare a global variable which will be outside of function and initiate it later in the function Commented Aug 3, 2022 at 16:16
  • 1
    You don't have any elements with class .select-section in that markup. Commented Aug 3, 2022 at 16:17
  • When I use: let x; outside the function I get undefined; I want to get its value in the console log. Commented Aug 3, 2022 at 16:19
  • 1
    move the console.log inside the function. Commented Aug 3, 2022 at 16:23
  • 1
    If you add in that missing div that code will work. But yes, that variable is scoped to the function, so move the log inside the function. But there isn't much point since the code works.... Commented Aug 3, 2022 at 16:24

3 Answers 3

2

Two options:

1

const selectSection = document.querySelector(".select-section");

let x;

function myFunction() {
  x = document.getElementById("my-select").value;
  selectSection.style.backgroundColor = x;
}

myFunction()

console.log(x);

2

const selectSection = document.querySelector(".select-section");

function myFunction() {
  const x = document.getElementById("my-select").value;
  selectSection.style.backgroundColor = x;
  return x
}

const x = myFunction()
console.log(x);

Remember to never use var

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

6 Comments

In your first example, the console.log(x) will execute before x gets its value.
The statement "Remember to never use var" is incorrect, there are still some uses where var should be used
Aside from golfing or legacy code, I don't know any. I can agree to never use var if you are a beginner
There is nothing wrong with var when you require function level scoping.
Why not use let then?
|
2

First please don't use the var keyword in your code read this article to know the reason

Second to solve this problem

  1. try to declare your variables in the global scope:

    let x; // in global scope (outside the function)

  2. and assign a value inside the function:

    x = document.getElementById("my-select").value;

Comments

0

You can declare the variable outside the scope of the function, but you must make sure to only access that variable AFTER the function has completed, otherwise the code will run BEFORE the function executes and the variable will not have been altered yet.

const selectSection = document.querySelector(".select-section");

let x = null;

function myFunction() {
  x = document.getElementById("my-select").value;
  document.body.style.backgroundColor = x;
  // If this line executes, we know x has been modified, so
  // it's safe to try to access it outside of the function
  showX();
}

function showX(){
  console.log(x);
}
<label for="my-select">Pick a color:</label>
<select name="" id="my-select" value="Choose the background color" onChange="myFunction()">
  <option value="default">default</option>
  <optgroup label="Warm Colors">
    <option value="red">red</option>
    <option value="orange">orange</option>
    <option value="yellow">yellow</option>
  </optgroup>
  <optgroup label="Cool Colors">
    <option value="green">green</option>
    <option value="blue">blue</option>
    <option value="purple">purple</option>
  </optgroup>
</select>

Comments

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.