Selected selector

0

How to enforce an if if a value of select is selected

I have this:

<select style="width: 120px; position: relative; left: 250px;" class="btn btn-warning btn-clan" onchange="document.getElementById('nick').value = $(this).val()">
<option disabled selected>CLAN</option>
<option value="TYT">TYT</option>
<option value="ZG">ZG</option>
<option value="ZT">ZT</option>
<option value="HERO">HERO</option>
<option value="MGR">MGR</option>
<option value="WARD">WARD</option>
<option value="ACE">ACE</option>
<option value="AG">AG</option>
<option value="sp">SUPERTANKER</option>
</select>

and I want that when choosing the value " ZT " the if occurs.

    
asked by Eduardo Campos 30.01.2017 в 06:32
source

2 answers

0

One option is to use the same event that is generated in onchange to perform the check, passing the selected value itself, which can be obtained from the property value of the element.

    function comprobarCondicion(opcion) {
      this.value = opcion;
      
      if (opcion == 'ZT') {
        console.log('La condicion ZT se cumple');
      }
    }
<select style="width: 120px; position: relative; left: 250px;" class="btn btn-warning btn-clan" onchange="comprobarCondicion(this.value)">
    <option disabled selected>CLAN</option>
    <option value="TYT">TYT</option>
    <option value="ZG">ZG</option>
    <option value="ZT">ZT</option>
    <option value="HERO">HERO</option>
    <option value="MGR">MGR</option>
    <option value="WARD">WARD</option>
    <option value="ACE">ACE</option>
    <option value="AG">AG</option>
    <option value="sp">SUPERTANKER</option>
    </select>
    
answered by 30.01.2017 в 08:18
0

You can not access the value directly. You have to do it through the property selectedIndex . That returns the position of the selected item in the list. To recover the value you just have to recover it from the select as well (for example): mySelect.options[mySelect.selectedIndex].value

Once you have the courage, you can do everything you need. Here is a small example of how you can retrieve it and show it on the console:

function valorCambiado(element) {
  console.log(element.options[element.selectedIndex].value);
  }
<select style="width: 120px; position: relative; left: 250px;" class="btn btn-warning btn-clan" onchange="valorCambiado(this)">
<option disabled selected>CLAN</option>
<option value="TYT">TYT</option>
<option value="ZG">ZG</option>
<option value="ZT">ZT</option>
<option value="HERO">HERO</option>
<option value="MGR">MGR</option>
<option value="WARD">WARD</option>
<option value="ACE">ACE</option>
<option value="AG">AG</option>
<option value="sp">SUPERTANKER</option>
</select>

I hope it serves you.

    
answered by 30.01.2017 в 08:14