0

I have this code:

<script>
var paesi = ["Austria","Belgium","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Ireland","Italy","Latvia","Lithuania","Luxembourg","Malta","Netherlands","Poland","Portugal","Romania","Slovakia","Slovenia","Spain","Sweden","United Kingdom"];
var sel = document.getElementById('country');
for (var i = 0; i < paesi.length; i++) {
     var opt = document.createElement('option');
     opt.innerHTML = paesi[i];
     opt.value = paesi[i];
     sel.appendChild(opt);
}

this array is in:

<div id="seleziona-nazione"><span><strong>Seleziona nazione</strong></span></div> 
<br /><br />  <br /><br />
<select id="country"></select>
<button id="button1">seleziona</button>

I want that when I click button1 the text in selectedbox was read and then I can do some actions. I try like this, but doesn't work:

function scelta() {
    document.getElementById("button1").addEventListener("click", function (paesi) {
        if (document.getElementById('country').text = 'Austria') {
        myLatLng = { lat: 47.516231, lng: 14.550072 };

         centra();
        };
    });
};

myLatLng is a global variables. Can someone help me?

Fanjo Lama
  • 581
  • 3
  • 15

4 Answers4

1

in the click event you should use value instead of text and use == instead of = in the condition

document.getElementById("button1").addEventListener("click", function (paesi) {

  if (document.getElementById('country').value == 'Austria') {
    myLatLng = { lat: 47.516231, lng: 14.550072 };

    centra();
  };

});

https://jsfiddle.net/egtLvwvw/1/

Ahmed Salama
  • 2,795
  • 1
  • 11
  • 15
1

You could do something like this:

var paesi = ["Austria","Belgium","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Ireland","Italy","Latvia","Lithuania","Luxembourg","Malta","Netherlands","Poland","Portugal","Romania","Slovakia","Slovenia","Spain","Sweden","United Kingdom"];

var sel = document.getElementById('country');
for (var i = 0; i < paesi.length; i++) {
  var opt = document.createElement('option');
  opt.innerHTML = paesi[i];
  opt.value = paesi[i];
  sel.appendChild(opt);
}


function doSomething(){
  var x = document.getElementById('country');
  var y = x.options[x.selectedIndex].text;
  if(y == 'Austria')
     console.log('Austria selected');

  }
<body>
  <div id="demo">
    <select id="country">
    </select>
    <input type="button" id="btnClick" onclick="doSomething()" value="Do something" />
  </div>
</body>

Also, I need to point out that, if you wish to compare values, you should use ' == ' for a non strict checking and ' === ' for strict, you could read more on '==' vs '===' here: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
iyerrama29
  • 496
  • 5
  • 15
1

you can write like this:

document.getElementById("button1").onclick = function() {
    if (document.getElementById('country').value == 'Austria') {
        myLatLng = { lat: 47.516231, lng: 14.550072 };

        centra();
    };
}
0

Try using onChange to get the selected value.

<div id="seleziona-nazione">
    <span><strong>Seleziona nazione</strong>    
    </span></div> 
  <br /><br />  <br /><br />
   <select id="country" onchange="getComboA(this)></select>
   <button id="button1">seleziona</button>

Inside the function getComboA you will get the selected value, whenever changing option in the dropdown.

function getComboA()
{
    //Keep the value in a variable. Use it on clicking on the button
}

Or you can just change as following code. use .value instead of .text.

  document.getElementById("button1").on("click",function (paesi) 
 {

      if (document.getElementById('country').value == 'Austria') 
      {
          alert("Australia");
      };

});
Shrabanee
  • 2,706
  • 1
  • 18
  • 30