0

How to replace the input attribute with jquery?
The code below:

<input class="input-radio" data-checkout-total-shipping="$ 25.00" /> 

I'm using Jquery which gives me a variable "total"
Jquery:

<script>  
var total = 0;
$(".product__description__property").each(function(d) {
  var value = $(this).text().replace("weight: ", "").replace(" lb", "");
  total += +value; 
});
document.getElementById('total').textContent = total;
</script>  

I want to replace whatever is inside " " of data-checkout-total-shipping="25" with total variable from Jquery script.

1 Answer 1

1

To replace data attribute with total value, you can use:

$('.input-radio[data-checkout-total-shipping]').attr('data-checkout-total-shipping', total);

So if total equals 50 e.g, it will give:

<input class="input-radio" data-checkout-total-shipping="50" /> 

If it is not expected behaviour, please elaborate...

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

2 Comments

I updated my question. I want to replace the 25 or whatever is there with my total. I don't have access to the <input /> section. The input is already given I want to replace this on client side so when they submit the form it will send total and not what is already inside the attribute
So what are you expecting as result? This e.g: data-checkout-total-shipping="$ 50.00". And i think you didn't understood this answer, this is replacing data attribute

Your Answer

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