Answer the question
In order to leave comments, you need to log in
What is wrong in js code?
There is a code:
<select name="orderStatus" id="orderStatus" class="main__order-meta-data__order-status">
<script>
function checkStatus(){
var status = document.getElementsByClassName("option").innerText;
console.log(status);
for (var i = 0; i < status.length; i++) {
if ("<?echo $orders_data['order_status']?>" == status[i]){
status[i].setAttribute('selected');
} else {
return false;
}
}
}
</script>
<option class="option" onload="checkStatus()">Новый</option>
<option class="option" onload="checkStatus()">Нужен доп звонок</option>
<option class="option" onload="checkStatus()">Готов к отправке</option>
<option class="option" onload="checkStatus()">Отправлен</option>
<option class="option" onload="checkStatus()">Доставлен</option>
<option class="option" onload="checkStatus()">Выкуплен</option>
</select>
Answer the question
In order to leave comments, you need to log in
If there is php, what else is there to sort through with javascript? Why is it impossible to put the selected attribute on the desired option via php?
<option class="option" <?if ($orders_data['order_status'] == "Новый"):?>selected<?endif?>>Новый</option>
Immediate error:
document.getElementsByClassName("option") // вернет коллекцию элементов а не один элемент
Try like this, status innerText is already on comparison
let status = document.getElementsByClassName('option');
console.log(status);
for (var i = 0; i < status.length; i++) {
if ("Новый" == status[i].innerText){
status[i].setAttribute('selected', '');
console.log(status[i].innerText);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question