-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>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • Do you have any other codes inside `BRICK_ROWS()`? – Chan Guan Yu Dec 05 '22 at 01:23
  • No, that's it.. – mightfloat Dec 05 '22 at 01: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 – Breezer Dec 05 '22 at 01:25
  • Thanks for the answer and the tips! I've changed it to let and it still doesn't updates based on my select value. – mightfloat Dec 05 '22 at 01:31
  • 1
    I see here got a similar solution to your question: [link](https://stackoverflow.com/questions/19329978/change-selects-option-and-trigger-events-with-javascript), please have a look – Chan Guan Yu Dec 05 '22 at 01:39
  • 1
    Does this answer your question? [Change – showdev Dec 05 '22 at 02:19

3 Answers3

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>
Gustavo
  • 11
  • 2
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;
});
asportnoy
  • 2,218
  • 2
  • 17
  • 31
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

Mohamed EL-Gendy
  • 566
  • 4
  • 11