Good afternoon, tell me, please, with such a task, there is a drop-down list of the type

<select id="my_select" name="my_select"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> 

It is necessary that when selecting an item in the list, a string variable is added with the string of the JS script, i.e. something like

  var prog = $("#my_select option:selected").val(); var path = "program" + prog; 

I am not strong in JS and therefore I don’t quite understand how to do it right, and the condition is that the page does not need to be reloaded.

Thank you in advance.

  • where was it added? $('#my_select').change(function(){var value=$(this).val();}); - zb '

2 answers 2

Well, what a fashion for every sneeze and a bunch of jkkery vachachivat? ..
Native - faster and more correct:

 var targetValue; // переменная, куда при изменении выбора списка уйдет его value var sel = document.getElementById("my_select"); sel.onchange = function() { targetValue = sel.value; }; 
  • one
    document.getElementById ("my_select"). onchange = function () {targetValue = this.value; } m, is it also better? - lampa
  • The jquery question so why the answer should not be on it? Of course, the native is very beautiful, but the question was asked with an example. - binliz
  • one
    @binliz and no tags :) - lampa
  • Yes, I’m better off with Dyz - but I left it more cumbersome to understand, because the vehicle itself admitted that it was not ready yet :) - deivan_
  • Yes, jQuery is not needed, just for now I understand only js, and the jQuery example was near, thanks for the help! - Rumato

something like this

 var path=""; jQuery(document).on('change','#my_select',function(){ path = "program" + jQuery(this).val(); }); 
  • Thank! Your example will come in handy;) - Rumato