0
<select id="curr">
          <option value="usd" selected>USD</option>
          <option value="eur">EUR</option>
          <option value="pnd">pound</option>
</select>
 <span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>
 <span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>
 <span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>

I wants if any one choose Eur then price should change to data-eur value.. or whatever they choose that price should update with that data-

3
  • Have you tried something with $("select#curr").on("change", ....) ? Commented Jan 21, 2021 at 10:29
  • NO not yet.... aany suggestion? Commented Jan 21, 2021 at 10:45
  • Yes, try something with $("select#curr").on("change", ....) Commented Jan 21, 2021 at 11:15

2 Answers 2

1

You can try something like this:

$('#curr').change(function() {
  var v = $(this).val();
  $("span.price").each(function() {
    $(this).text($(this).attr("data-"+ v))
  })
});

Demo

$('#curr').change(function() {
  var v = $(this).val();
  $("span.price").each(function() {
    $(this).text($(this).attr("data-"+ v))
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="curr">
  <option value="usd" selected>USD</option>
  <option value="eur">EUR</option>
  <option value="pnd">pound</option>
</select>
<span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>
<span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>
<span class="price" data-usd="$1,500" data-eur="£1.271" data-pnd="€1.000">$1,500</span>

Sign up to request clarification or add additional context in comments.

Comments

1

Just write some JS code:

const currencySelector = $("#curr");
const elements = $('.price');
const currencies = {
  'usd': 1500,
  'eur': 1.271,
  'pnd': 1000
}

currencySelector.on('change', (event) => {
const value = event.target.value;
  elements.each((index, el) => {
    $(el).html(currencies[value]);
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.