In the span output data paramert options?

There is for example an array of options to select
<select name="payments">
 <option name="delivery" value="1" id="delivery_1" data-payments="[1]" data-price="10">delivery 1</option>
 <option name="delivery" value="2" id="delivery_2" data-payments="[2]" data-price="20">delivery 2</option>
 <option name="delivery" value="3" id="delivery_3" data-payments="[3]" data-price="30">delivery 3</option>
</select>
<span> shipping Cost:<span class="price"> (data-price)</span></span>


How do I pass in a span with class "price" that he would have changed depending on the selected option.

Tried that's the way to pull old but understand that this is not quite right.

$(document).ready(function(){
 $('span').attr('data-price', $('option[name="delivery"]').data('price'));
});
April 7th 20 at 15:28
2 answers
April 7th 20 at 15:30
Doubling down on the question? =)
Duplicate response:

April 7th 20 at 15:32
$(document).ready(function(){
$('select[name="payments"]').on('change',function(){
var val = $(this).val();
var price = $(this).find('option[value="'+val+'"]').data("price");
$('span.price').html(price);
});
});

Find more questions by tags HTML