Skip to content
Advertisement

How can I solve this in Google Apps Script?

I am trying to extract which option has been selected from the code below in Apps Script. The idea is to change the value of the global variable column depending on which element is selected, but it is not working.

html

<select class="form-select" id="seleccion">
 <option value="1">One</option>
 <option value="2">Two</option>
</select>

java

var column;
function optionSelec(){
var e = document.getElementById("seleccion");
var strUser = e.options[e.selectedIndex].text;

if (strUser == 1){column = [1]};
if (strUser == 2){column = [2]};
}

Thanks a lot!

Advertisement

Answer

var strUser = e.options[e.selectedIndex].text;

…should be:

var strUser = e.options[e.selectedIndex].value;
                                     //  👆 

See it working:

let column;

function optionSelect() {
  const el = document.getElementById("seleccion");
  column = [+el.options[el.selectedIndex].value].filter(o => o);
  console.log(column);
}

document.getElementById('seleccion').addEventListener('change', optionSelect);
optionSelect();
<select class="form-select" id="seleccion">
  <option></option>
  <option value="1">One</option>
  <option value="2">Two</option>
</select>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement