THIS IS NOT A DUPLICATE BECAUSE I USE .data() and NOT .attr() LIKE ATTACHED SUGGESTS
I need to change a data attribute value when a variable is updated. I'm making a dropdown that ties to a discount field and I need to update both the text and the data attribute that is in the <option> tag.
The relevant pieces looks like this:
$(function() {
$("#plan-options").change(function() {
var moneyAmount = $(this).find('option:selected').data('amount');
$("#selected-price").text("$" + moneyAmount);
$("#purchase-warning").toggle();
$(".default-encouragement").toggle();
});
});
#plan-options is a <select> tag with <option>s that have data-attributes.
after that:
...
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let amountOffMoney = (data.amount_off / 100).toFixed(2);
if (data.percent_off != null) {
selectedAmount = selectedAmount * (100 - data.percent_off) / 100
} else if (data.amount_off != null) {
selectedAmount = selectedAmount - amountOffMoney;
// this variable doesn't update - why?
$("select option:selected").data("amount", selectedAmount);
} else {
alert("Someting wrong happened!");
return;
}
The most relevant piece is here:
selectedAmount = selectedAmount - amountOffMoney;
$("select option:selected").data("amount", selectedAmount);
My expectation is that I assign selectedAmount a new value, and changing the data attribute to selectedAmount should change to the new assignment but that is not happening. The value stays the same.
Is it because it's let? Is it a scoping issue?
FULL CODE SNIPPET:
$(function() {
$("#plan-options").change(function() {
var moneyAmount = $(this).find('option:selected').data('amount');
$("#selected-price").text("$" + moneyAmount);
$("#purchase-warning").toggle();
$(".default-encouragement").toggle();
});
});
...
jQuery(function ($) {
$(document).on("click",'.apply_coupon', function (e) {
e.preventDefault();
let plan = $('select[name="plan"]').val();
let coupon = $('input[name="coupon"]').val();
$.get('/premium/coupon/', {
coupon: coupon,
plan: plan
}, function (data) {
console.log("got data from calling coupon api:", data)
if (data.success) {
//discounted amount display handling
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let amountOffMoney = (data.amount_off / 100).toFixed(2);
if (data.percent_off != null) {
selectedAmount = selectedAmount * (100 - data.percent_off) / 100
} else if (data.amount_off != null) {
selectedAmount = selectedAmount - amountOffMoney;
console.log(selectedAmount);
$("#plan-options option:selected").data("amount", selectedAmount);
} else {
alert("Someting wrong happened!");
return;
}
let regex = /\d+\.*\d*/;
let newStr = selectedStr.replace(regex, selectedAmount.toString());
selected.text(newStr);
$('.apply_coupon').text("Remove");
$('.apply_coupon').addClass("remove_coupon").removeClass('apply_coupon');
$('input[name="couponVerified"]').val(1);
$('input[name="coupon"]').hide();
$('.coupon-results').show();
$('.coupon-results__code').text(data.name);
$('.coupon-results__info').text("$" + amountOffMoney + " off " + data.duration);
$("#selected-price").text("$" + selectedAmount);
} else {
alert(data.message);
}
})
});
$(document).on('click','.remove_coupon', function (e) {
e.preventDefault();
$.get('/premium/coupon/remove/', function (data) {
if (data.success) {
//discounted amount reverting handling
let selected = $("select option:selected");
let selectedAmount = selected.data("amount");
let selectedStr = selected.text();
let regex = /\d+\.*\d*/;
let newStr = selectedStr.replace(regex, selectedAmount.toString());
selected.text(newStr);
$('.remove_coupon').text("apply");
$('.remove_coupon').addClass("apply_coupon").removeClass('remove_coupon');
$('input[name="couponVerified"]').val(0);
$('input[name="coupon"]').show();
$('.coupon-results').hide();
$("#selected-price").text("$" + selectedAmount);
}
});
});
});