There is an array of data from the database, which I will iterate

foreach ($arr as $key => $value) { <select class="active" name="active"> <option><?=$value['active']?></option> <option class="sec_act"></option> </select> } 

It turns out one option empty, and the other has either active or noactive How can I use js to .active over the .active and fill in the empty option , where there is already an active one, add noactive and vice versa?

I had about such an attempt:

 $('.active').each(function(){ if($(this).val() == 'active'){ $(this).closest('.sec_act').html('noactive'); }else{ $(this).closest('.sec_act').html('active'); } }); 
  • It is absolutely incomprehensible what is at the entrance and what should be obtained at the exit. If you need a solution for js, why give the code in php? Add the final markup. - Grundy
  • document.getElementsByClassName w3schools.com/jsref/met_document_getelementsbyclassname.asp on jQuery in my opinion so $(".className") - nick_n_a
  • Instead of closest, it was necessary for the children)) and everything works - Alexander Reizan

1 answer 1

 $('.active option:first-child').each(function (index, option) { if ($(option).val() == 'active') $(option).closest('select.active').find('option.sec_act').text('noactive') else $(option).closest('select.active').find('option.sec_act').text('active') })