There is a group of switches that turn dark gray when choosing a switch :check .

Everything works well as long as the submission form on the page is filled out correctly, but as soon as it makes a mistake and the page reloads with error notification, the group of switches ceases to turn dark gray when the switch is selected.

How can this be fixed?

Code:

 jQuery.noConflict(); jQuery(document).ready(function($) { let className = 'label-active'; let $inputs_time = $('#order_time_field input[type=radio]'); $inputs_time.change(function(ev) { $inputs_time.parent('label').removeClass(className); $(this).parent('label').addClass(className); }); }); 
 .form-row label.label-active { background-color: #e4e4e4; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-priority="20" class="form-row form-row-wide" id="order_time_field"> <fieldset> <legend>ВрСмя доставки</legend><label><input type="radio" name="order_time" value="Π‘Π»ΠΈΠΆΠ°ΠΉΡˆΠ΅Π΅"> Π‘Π»ΠΈΠΆΠ°ΠΉΡˆΠ΅Π΅</label><label><input type="radio" name="order_time" value="К ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡƒ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ"> К ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡƒ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ</label></fieldset> </div> 

  • in theory, there should be an error in the console at the moment when dyeing does not work, look what is there. - Maxim Stepanov
  • @ Maxim Stepanov nea, there are no errors in the console - Vasya
  • it only comes to mind that when reloading, jQuery(document).ready(function($) is overridden somewhere , Maxim Stepanov

1 answer 1

This often happens if for some reason there are several DOM elements with the same id on the page. Because you select items by id

 $('#order_time_field input[type=radio]') 

check their uniqueness. It is better to form a class selector.

And for a complete answer, I would like to see this scenario somewhere completely.

  • I do not agree with what is better in class. This approach is called "just work", but it can lead to errors that are not immediately visible. The class is better when it is due to the task, but not everywhere where it fell. - Maxim Stepanov