AustinT
AustinT

Reputation: 2026

Select an option default by giving a value to select

Hello is there anyway to have a value selected by giving in a value?

<select name="color">
    <option value="black">black</option>
    <option value="white">white</option>
    <option value="red">red</option>
</select>

For example if I want black to be selected I would set selected = black.
Rather than using <option value="black selected>black</option>

Thank you for assistance, please let me know if there is any misunderstanding.

Upvotes: 0

Views: 161

Answers (2)

HENG Vongkol
HENG Vongkol

Reputation: 941

If you want to work with multiple select options, you can use this:

$("#color").val(["black"]);
// for single select option
$("#color").val(["black","red","white"]);
// for multiple select options

Upvotes: 0

Igor
Igor

Reputation: 15893

If you are not averse to using js and jquery:

<select name="color" id="color">
    <option value="black">black</option>
    <option value="white">white</option>
    <option value="red">red</option>
</select>

<script type="text/javascript">
$(document).ready(function () {
  $("#color").val("black");
});
</script>

Upvotes: 1

Related Questions