How to pass the current value of the selected select to onclick? For example from

<select name="option[227]"> <option value="">Выберите Опцию</option> <option value="19">Опция 3 </option> <option value="18">Опция 2 </option> <option value="17">Опция 1 </option> </select> 

In the <a href="#" onclick="add('Значение select');">+</a>

    2 answers 2

    If right on the "bare" JS + onclick , then something like this:

     function add(selectEl){ var value = (selectEl) ? selectEl.options[selectEl.selectedIndex].value : null; console.log(value); } 
     <select id="test"> <option selected="selected" value="0">0</option> <option value="2">2</option> </select> <a href="#" onclick="add(document.querySelector('#test'))">Test</a> 

    In this case, it is not the value that is passed to the function, but the list item itself, otherwise it is cumbersome to write directly in the onclick attribute.

    But it is better to use + jQuery events.

      JQuery example

       <select id="myselect"> <option value="1">Mr</option> <option value="2">Mrs</option> <option value="3">Ms</option> <option value="4">Dr</option> <option value="5">Prof</option> </select> 
       function add() { var a = $( "#myselect" ).val(); console.log(a); } 
      • one
        Then $ ("#myselect option: selected") .val (); - Alexey Lemesh