0

I have the following line of code:

tpj("#totalcostinclship").empty().append( "<span data-src='"+pricefinal+"' id='totalcostinclship'></span>" ).text(pricefinal);

It first empties the existing element and then appends the new element. pricefinal in this case contains 245 but when I inspect my HTML, this is what I see:

<span data-src="220" id="totalcostinclship">245</span>

The .text is applied but the data-src not at all. Why could that be? 220 is the value of the data-src on page load, so before any jQuery event is started.

1
  • 1
    Assuming tpj is a reference to jQuery, then I am unable to replicate your problem. The given code works absolutely fine: jsfiddle.net/q29Lgwu5. I can only assume you have some other logic which affects the data attribute after you append the new element Commented May 15, 2019 at 10:24

1 Answer 1

2

var pricefinal = 245;
var tpj = $.noConflict();

tpj("#totalcostinclship").empty().append("<span data-src='" + pricefinal + "' id='totalcostinclship'></span>").text(pricefinal);
tpj("#totalcostinclship").attr('data-src',pricefinal);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="totalcostinclship"></div>

Add this line

tpj("#totalcostinclship").attr('data-src',pricefinal);

after your js. Like this ->

tpj("#totalcostinclship").empty().append( "<span data-src='"+pricefinal+"' id='totalcostinclship'></span>" ).text(pricefinal);
tpj("#totalcostinclship").attr('data-src',pricefinal);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this did the job!

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.