1

I have an object literal with a list of countries with two values, price and name: enter image description here

When the select changes I store the values of the current selected country: enter image description here

And I store this value in the variable inner function .change():

current_country = $('#country').val();

I accessed this way the object literal for example countries_list.deu

But when I use my current_country Variable console shows me what undefined:

 console.log(countries_list.current_country);

How I can fix this?

Funny Frontend
  • 3,807
  • 11
  • 37
  • 56

2 Answers2

0

Use selectedIndex to get the selected value index and use .options[i].value to get the value of the option with the following index:

var countries = document.getElementById("country");
var current_country = countries.options[countries.selectedIndex].value;
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

you should be using bracket notation

console.log(countries_list[current_country]);

In your case, it actually looks for the property named current_country within the object, where it doesn't exist.

Look at this for better understanding of two different notations

Community
  • 1
  • 1
code-jaff
  • 9,230
  • 4
  • 35
  • 56