2

need some help here please ,

Basically what I've at this point is a script a user here shared with me , what it does is that it has created a hidden paragraph and the content displayed on this paragraph is conditioned by the values of 2 dropdowns , it works absolutely fine , there is only one thing though :

for the first < select > I want to use numbers as values : 0 for Audi , 1 for BMW , 2 for Mercedes and so on , but once I replace those values with numbers it doesnt seem to work ,

Please check the code , any help is more than appreciated , really :)

function myFunction() {
  let messages = {
    AudiBlack: 'Black Audi',
    AudiRed: 'Red Audi',   
    BMWBlack: 'Black BMW',
    BMWRed: 'Red BMW',
    MercedesBlack: 'Black Mercedes',
    MercedesRed: 'Red  Mercedes',
    VolvoBalck: 'Black Volvo',
    VolvoRed: 'Red Volvo'
  };
    var car = document.getElementById("mySelect").value;
    var color = document.getElementById("Variants").value;
    document.getElementById("demo").innerHTML = `${messages[car+color]}`;
}
<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>
	<select id="Variants" onchange="myFunction()">
	 	<option value="Black"> Black
	  	<option value="Red">Red
	</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>
</body>

2 Answers 2

3

You have to redeclare your keys in myFunction() so that it works:

function myFunction() {
  let messages = {
    '0Black': 'Black Audi',
    '0Red': 'Red Audi',   
    '1Black': 'Black BMW',
    '1Red': 'Red BMW',
    '2Black': 'Black Mercedes',
    '2Red': 'Red  Mercedes',
    '3Black': 'Black Volvo',
    '3Red': 'Red Volvo'
  };
    var car = document.getElementById("mySelect").value;
    var color = document.getElementById("Variants").value;
    document.getElementById("demo").innerHTML = `${messages[car+color]}`;
}
<body>
	<p>Select a new car from the list.</p>
	<select id="mySelect" onchange="myFunction()">
	  	<option value="0">Audi
	  	<option value="1">BMW
	  	<option value="2">Mercedes
	  	<option value="3">Volvo
	</select>
	<select id="Variants" onchange="myFunction()">
	 	<option value="Black"> Black
	  	<option value="Red">Red
	</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>
</body>

And be careful you have a VolvoBalck instead of VolvoBlack in your code.

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

2 Comments

man thank you so much , apparently forgot to put brackets around : 0Black and so on , that's why it didnt work , its working just fine now , thanks a million !
I'm glad I could help. You could mark the answer as correct if you consider it is.
0

the easiest way of having the values in your select as numbers is using an array, very similar to what you are doing, however, I would change the nature of the array to contain objects. Like this

function myFunction() {

  let messages = [{
      red: 'Red Audi',
      black: 'Black Audi'
    },
    {
      red: 'Red BMW',
      black: 'Black BMW'
    }
  ];
  
  var carIdx = document.getElementById("mySelect").value;
  var color = document.getElementById("Variants").value;
  
  document.getElementById("demo").innerHTML = `${messages[carIdx][color]}`;
}
<body>
  <p>Select a new car from the list.</p>
  <select id="mySelect" onchange="myFunction()">
    <option value="0">Audi
      <option value="1">BMW
  </select>
  <select id="Variants" onchange="myFunction()">
    <option value="black"> Black
      <option value="red">Red
  </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>
</body>

The reason I'd change it to be that way is in case you have different colors for different cars and is also easier to read and understand.

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.