Hello everyone, the task is to write a condition on javascript for the situation when the user selects a group, presses the "forward" button and depending on his choice one or another page (for example, a list of this group) pulls up.

I perfectly understand the logic of the work, but due to lack of experience, the problem is this: I don’t know how to pull up the group’s value (I tried the ID, for some reason it worked out, I spent a lot of time and finally decided to write here.) And what value the variable should take , if variable var == 0 for the drop-down list (can it take such a value at all or should it be null)

Please write the correct version and, if it is not difficult, CLEAR what and how , reviewed a bunch of videos, a bunch of articles, found an option only like this:

var val = getElementById('one').value; if val == 0 document.location.href = spisokgryppi.html '; else alert("Данный код в баззе не хранится!");) 
 <!DOCTYPE> <HTML> <HEAD> <meta charset="utf-8"> <script type="text/javascript"> -- </script> </head> <body> <form> <h4>Номер группы</h4> <select> <option disabled>Номер группы</option> <option id = "one" >07-16</option> <option id = "two" >06-14</option> <option id = "three">06-15</option> </select> </form> <form 1> <input type="submit" value="Вперед"> </form 1> </body> </HTML> 

  • I perfectly understand the logic of the work - describe it in your own words - Grundy
  • I have to apply 3 functions that will check the following conditions when I click on the “Forward” button: 1) if val = '1' (i.e. selected by the user) for id = 'one' , then the link to href = spisokgryppi 1 .html. 2) if val = '1' (i.e. selected by the user) for id = 'two' , then go to the link href = spisokgryppi 2 .html. 3) if val = '1' (i.e. selected by the user) for id = 'three' , then go to the link href = spisokgryppi 3 .html. - Gustaf Hellström
  • You can edit the question with the help of the button, and edit the question - Grundy

1 answer 1

 <body> <form> <h4>Номер группы</h4> <select id="mySelect"> <option disabled>Номер группы</option> <option value="1" >07-16</option> <option value="2" >06-14</option> <option value="3">06-15</option> </select> </form> <button id="myButton" onClick="myButtonClickHandler()" value="Вперед"></button> </body> function myButtonClickHandler(event) { var mySelect = document.getElementById('mySelect'); var value = mySelect.value; document.location.href = "spisokgryppi" + value + ".html"; } 

The condition is not necessary here, just by clicking on the button, we take the current value from the form and redirect to the generated link. Corrected for the script described in the comment

  • why not mySelect.value ? - Grundy
  • @Grundy, yes so better, I will correct - Spaider
  • Thank you very much, everything turned out even easier than I thought. - Gustaf Hellström
  • @ GustafHellström Not for anything, successful development) - Spaider