-2

I want to change the value of a variable via a HTML select prompt but it's not working.

// starting number of brick rows
const BRICK_ROWS = document.getElementById('nivel');
<select id='nivel' onchange='BRICK_ROWS()'>
  <option value="1">easy</option>
  <option value="2">medium</option>
  <option value="3">hard</option>
</select>

6
  • Do you have any other codes inside BRICK_ROWS()? Commented Dec 5, 2022 at 1:23
  • No, that's it.. Commented Dec 5, 2022 at 1:25
  • Please add details and clarity to your question for anyone to be able to help you, what do you want to achieve, how are you trying to achieve this, what results are you getting. at a first glance I can tell you directly as it is right now BRICK_ROWS is not a function and if a variable is meant to be changed to define it as a const use let, I would recommend going through a tutorial about the basics of javascript Commented Dec 5, 2022 at 1:25
  • 1
    I see here got a similar solution to your question: link, please have a look Commented Dec 5, 2022 at 1:39
  • 1
    Does this answer your question? Change <select>'s option and trigger events with JavaScript Commented Dec 5, 2022 at 2:19

3 Answers 3

1

Create a non-constant variable to store the value:

let difficulty; // you can give it a default value if you want

Then, use .addEventListener() to listen for changes and update your variable:

const difficultySelector = document.getElementById('nivel');
difficultySelector.addEventListener('change', () => {
    difficulty = difficultySelector.value;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You have a some syntax and logical error in your code.

  1. const declaration mean that you can't change the value through your code once you set it.

  2. you're trying to call variable lie a function using 'onChange' event

The solution:

  1. declare your variable as a dynamic variable

  2. create a function to reset its value

let BRICK_ROWS = 0;
function SET_BRICK_ROWS() {
BRICK_ROWS = document.getElementById('nivel').value; 
console.log(BRICK_ROWS)
}
 <select id='nivel'  onchange='SET_BRICK_ROWS()'>
        <option value="1">easy</option>
        <option value="2">medium</option>
        <option value="3">hard</option>
      </select>

Note: You can use addEventListener If you want a better solution

Comments

1

First: you can`t declare a const and then change this value. You should declare a var or let. Then to get select value:

<select id="nivel" onchange="changeValue(event)">
  <option value="1">easy</option>
  <option value="2">medium</option>
  <option value="3">hard</option>
</select>
<script>
  var brickRowsValue = 0
  function changeValue(event) {
    brickRowsValue = event.target.value
  }
</script>

1 Comment

This doesn't actually change the value when the option is updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.