-1

Below is my option

<select name="currency" id="currency">
  <option value="AUD">AUD</option>
  <option value="BDT">BDT</option>
  <option value="EUR">EUR</option>
  <option value="JPY">JPY</option>
  <option value="USD">USD</option>
  <option value="USDT">USDT</option>
</select>

How do I set USD as the default value? (This default value can be dynamically changed, so it cannot hard code set USD as the default value)

The solution that I had tested : $('select[name="currency"] option:contains("USD")').prop('selected', true); but it will show USDT instead of USD

Any idea?

trincot
  • 317,000
  • 35
  • 244
  • 286
Alan Tang
  • 31
  • 9

3 Answers3

1

try this one

<select name="currency" id="currency">
  <option value="AUD">AUD</option>
  <option value="BDT">BDT</option>
  <option value="EUR">EUR</option>
  <option value="JPY">JPY</option>
  <option value="USD" selected>USD</option>
  <option value="USDT">USDT</option>
</select>
Zulfikar
  • 49
  • 6
  • 1
    Directly from the question: "*This default value can be dynamically changed, so it cannot [be] hard code[d]"* – freedomn-m Aug 01 '22 at 08:09
1

If by dinamically changed you mean using js:

const dynamicDefaultJsVal = 'USD';

$('#currency').val(dynamicDefaultJsVal); // jQuery

// document.querySelector('#currency').value = dynamicDefaultJsVal; // Vanilla js
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="currency" id="currency">
  <option value="AUD">AUD</option>
  <option value="BDT">BDT</option>
  <option value="EUR">EUR</option>
  <option value="JPY">JPY</option>
  <option value="USD">USD</option>
  <option value="USDT">USDT</option>
</select>
Mauro Aguilar
  • 1,093
  • 13
  • 22
0
$('#currency').val("USD");
tripleee
  • 175,061
  • 34
  • 275
  • 318
Alan Tang
  • 31
  • 9
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Aug 01 '22 at 14:10
  • Is this supposed to be an answer or an update to your question? In the first case, please read [answer] and then [edit] the answer to clarify. In the second case, please delete this and edit your question instead. – ChrisGPT was on strike Aug 01 '22 at 17:54