-1

The current code I have is as follows:

function SetDefaultCarrier(orderCarrier) {
    $("#carrierdropdown option:contains(" + orderCarrier + ")").attr('selected', true);
}

The issue is that this will select the first option found in the list which contains the string orderCarrier. For instance, if orderCarrier is "UPS IG", the function will sometimes select the value "UPS IG C" since the former is a substring of the latter. How do I make it only select an exact match?

  • Use `.val(orderCarrier)` https://stackoverflow.com/questions/4864620/how-to-use-jquery-to-select-a-dropdown-option – Jasen Oct 12 '21 at 19:12
  • @Jasen Oh, how dumb I feel. Thank you! – John Bouchard Oct 12 '21 at 20:00
  • Does this answer your question? [How to use jQuery to select a dropdown option?](https://stackoverflow.com/questions/4864620/how-to-use-jquery-to-select-a-dropdown-option) – freedomn-m Oct 13 '21 at 04:11

1 Answers1

0

As commented, the following is what I was looking for:

function SetDefaultCarrier(orderCarrier) {
    $("#carrierdropdown").val(orderCarrier).attr('selected', true);
}
  • The correct way to set the selected attribute is to use the [jQuery .prop()](https://api.jquery.com/prop/), like so: `$("#carrierdropdown").val(orderCarrier).prop('selected', true);` – Aidan Hakimian Oct 12 '21 at 20:44