Good day. How can I select the appropriate option in select when clicking on one of the rows in the table? i.e. click on line 3 - option 3 will be selected, etc.
1 answer
document.querySelector("table").addEventListener('click', function (e) { var td = e.target.closest("td[data-val]") if (td) { document.querySelector("select").value = td.dataset.val } }) table { display: inline-table; } table, tr, td { border: 1px solid; } td { cursor: pointer; } <table><tr><td data-val=1>First<td data-val=2>Second<td data-val=3>Third</tr></table> <select><option value=1>Первый<option value=2>Второй<option value=3>Третий</select> |