JSFiddle - React, Tailwind, and code Playground
how to get a value from a table and set value a select/option
by Akram kamal
HTML
<table id="myTable">
<thead>
</tr>
<th>Thing</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>shirt</td>
<td>
<button type="button" class="quickSelect">Select ME</button>
</td>
</tr>
<tr>
<td>table</td>
<td>
<button type="button" class="quickSelect">Select ME</button>
</td>
</tr>
</tbody>
</table>
<select id="myselect">
<option value="-100">Select...</option>
<option value="-101">***NEW***</option>
<option value="1">apple</option>
<option value="2">shirt</option>
<option value="3">table</option>
</select>
JavaScript
$(document).ready(function () {
$('.quickSelect').on('click', function () {
var $wRow = $(this).closest("tr");
var $mything = $wRow.find("td:nth-child(1)");
console.log($mything.text());
$('#myselect option').each(function () {
this.selected = (this.text == $mything.text());
});
});
});