this is my HTML file
<body>
<form id="myform" style="width:200px;margin:0 auto" >
<select id="selectCountry" class="btn btn-secondary dropdown-toggle">
<option>یک کشور را انتخاب کنید</option>
</select>
<button type="button" class="btn btn-danger" onclick="myFunction()">Try it</button>
</form>
<script src="./script.js"></script>
<script>
function myFunction() {
var x = document.getElementById("selectCountry").selectedIndex;
var result = document.getElementsByTagName("option")[x].value;
console.log(result); // this line gives [Object object]
}
</script>
</body>
console.log(result); gives back [object Object] no matter what i do.`
and this is my .js file
const api_url = 'https://api.covid19api.com/summary';
var dropDownSelector = document.getElementById('selectCountry');
var api_countries = [];
var api_getCountrySpecifics = [];
// Initializing The Api ///
async function getData(url) {
const response = await fetch(url);
const data = await response.json();
return data;
}
getData(api_url).then(response => {
for( var i=0 ; i < 190 ; i++ ) {
api_countries.push(response.Countries[i].Country);
api_getCountrySpecifics.push(response.Countries[i]);
}
for(var j = 0 ; j < 190 ; j++) {
var opt = api_countries[j];
var specifics = api_getCountrySpecifics[j]
var el = document.createElement("option");
el.textContent = opt;
el.value = specifics;
dropDownSelector.appendChild(el);
}
});
Does anyone know what's the problem? it is very important to me to solve this. thank you beforehand.
el.value = specifics;assigns an object to an html attributespecifics.Countrywould be goodresponse.Countries[i]is an object since you push itsCountryproperty onapi_countries. It all boils down to... What do you want to do with this object.