Help to deal with the script. there is such a script

<input class="catSelector9" checked="checked" type="hidden" value="2" name="ocat" id="cus2"> <select class="car_Marka" id="car_Marka" onchange="run()"> <option id="cus2" value="2">AC</option> <option id="cus3" value="3">ACURA</option> <option id="cus5" value="5">ADLER</option> <option id="cus4" value="4">AUDI</option> </select> <script> function run(){ document.getElementById ("cus2").value = document.getElementById("car_Marka").value; } </script> 

When option is selected, only the input value changed. But it is necessary for me, that at a choice of option , input value and input id changed.

Here I realized that the first time the input id changed, the script does not work, but can this execution be done using the input class and select class ?

    2 answers 2

    JQuery can be used to make a selection using the class

     $(".car_Marka").on("change", function() { var selected_option = $(".car_Marka option:selected"); $(".catSelector9") .attr("id", selected_option.attr("id")) .attr("value", selected_option.attr("value")); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <input class="catSelector9" checked="checked" type="hidden" value="2" name="ocat" id="cus2"> <select class="car_Marka" id="car_Marka"> <option id="cus2" value="2">AC</option> <option id="cus3" value="3">ACURA</option> <option id="cus5" value="5">ADLER</option> <option id="cus4" value="4">AUDI</option> </select> 

    • Thanks for the help! - Samir Mamedov
    • but for some reason the page does not work. On the computer, I will insert a tes.html weight code from this page avtousta.ucoz.com/board/0-0-0-0-1 and your code that was altered. I keep opening in the browser chrome works fine. I copy the same code into the page, but it doesn’t work. - Samir Mamedov
    • Thank you, your script on the function class works fine, but only in a normal browser during the test, and when loading the page does not work. Please look here at this script <div id="ad-car-detail-content"> </div> <script type="text/javascript" charset="utf-8" src="http://avtousta.ucoz.com/js/board/add-cars-board.js"></script> - Samir Mamedov

      <div id="ad-car-detail-content"> </div> <script type="text/javascript" charset="utf-8" src="http://avtousta.ucoz.com/js/board/add-cars-board.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script>$("select.car_Marka").on("change", function() {var selected_option = $("select.car_Marka option:selected");$("input.catSelector9").attr("id", selected_option.attr("id")).attr("value", selected_option.attr("value"));});</script> 

    DmitryScaletta: Thanks, thanks! but the code on the page does not work! and on the computer when the test works fine