JSFiddle - React, Tailwind, and code Playground
HTML
<table border="1">
<tr>
<td style="background-color: #eee;">
<b>Mailing Address</b>
<br /><br />
<form>
Name:
<input type="text" name="shippingname" />
<br />
City:
<input type="text" name="shippingcity" onchange="FillBilling(this.form);" />
<br />
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form);" />
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<br /><br />
<b>Billing Address</b>
<br /><br />
Name:
<input type="text" name="billingname" />
<br />
City:
<select name="billingcity">
<option value="City 1">City 1</option>
<option value="City 2">City 2</option>
<option value="City 3">City 3</option>
</select>
</form>
</td>
</tr>
</table>
JavaScript
function FillBilling(f) {
if (f.billingtoo.checked === true && f.shippingcity.value) {
f.billingname.value = f.shippingname.value;
var found = false;
for (var i = 0; i < f.billingcity.options.length; i++) {
if (f.billingcity.options[i].value.toLowerCase() === f.shippingcity.value.toLowerCase()) {
f.billingcity.selectedIndex = i;
found = true;
break;
}
}
if (!found) {
var extraOption = f.billingcity.getAttribute("data-extra-option");
if (extraOption) {
f.billingcity.options[f.billingcity.options.length - 1].text = f.shippingcity.value;
f.billingcity.options[f.billingcity.options.length - 1].value = f.shippingcity.value;
} else {
var newOption = new Option(f.shippingcity.value, f.shippingcity.value);
f.billingcity.setAttribute("data-extra-option", "true");
f.billingcity.appendChild(newOption);
f.billingcity.selectedIndex = f.billingcity.options.length - 1;
}
} else {
if (f.billingcity.getAttribute("data-extra-option")) {
f.billingcity.removeChild(f.billingcity.options[f.billingcity.options.length - 1]);
f.billingcity.selectedIndex = 0;
}
}
} else {
if (f.billingcity.getAttribute("data-extra-option")) {
f.billingcity.removeChild(f.billingcity.options[f.billingcity.options.length - 1]);
f.billingcity.selectedIndex = 0;
}
}
}