<select name="third_rubric" id="third_rubric"> <option value="00">Выбор подрубрики</option> <option value="110101">Компьютеры</option> <option value="110107">Куплю</option> <option value="110102">Мониторы</option> <option value="110103">Ноутбуки</option> <option value="110104">Принтеры</option> <option value="110106">Прочее</option> <option value="110105">Сканеры</option> </select> 

I would very much like the script to automatically select from the drop-down list "Computers"

Here, for example, I am writing this code to check if all these .options work in a monkey:

 var select, value, text; select = document.getElementById("third_rubric"); // Выбираем select по id value = select.options[0].value; // Значение value для выбранного option text = select.options[0].text; // Текстовое значение для выбранного option alert("Value: " + value + "\nТекст: " + text); // Вывод алерта для проверки значений 

The console displays an error. TypeError: Cannot read property 'options' of null

Help, give a link to the script - an example. I myself searched for 2 days. In JS, I am zero.

    1 answer 1

     var select; select = document.getElementById("third_rubric"); // Выбираем select по id for(i=0; i < select.options.length; i++) { if(select.options[i].text == "Компьютеры") { select.options[i].setAttribute('selected',true) } }