Replace value after tag, but inside div

Asked

Viewed 28 times

0

I have the HTML code, and I need to replace the decimal value at the end of the div, the project is doing a reverse proxy of a site, and I have to put the value of the tag "changed" in the place where is the "11.00" the HTML is this:

<div class="osg-outcome__price" changed="7.14">
    <i class="osg-outcome__trend"></i>
    11.00
</div>

I’ve tried to do it this way:

var value = parseFloat($(this).text());
var ht = $(this).html().replace(value,$(this).attr("changed"));
$(this).html(ht);

But the problem is that the value of the div changes constantly based on a websocket, so when I change the value that way, the websocket stops updating the div for some reason.
The most I could was to put the value of interest inside the tag 'changed', now I need to take this value to div without touching the <i class="osg-outcome__trend">.

Is there a way to do this in CSS? I think I would avoid these problems. Or a solution jQuery more precise?

  • Try to put the value 11.00 in some tag (span, p, div) and put an id or class there in the variable ht call for this id or class.

1 answer

1

Adds .toFixed(2) at the value in this line of replace:

var ht = $(this).html().replace(value.toFixed(2),$(this).attr("changed"));

The parseFloat dispenses with decimal zeros:

11.00 --> 11
11.10 --> 11.1

So the replace will not work because you will not find the value correctly. The toFixed(2) will keep zeroes too:

11.00 --> 11.00
11.10 --> 11.10
  • Thanks, but not yet! so I did the following, I hid the original value with $(this).css("display","none"); and then added a new div with the value $(this).parent("div").append("<div id='changed'>"+baixado+"</div>"); then to update the value I used a condition that checks if the changed id div exists, if there is it updates, if there is no it creates.

  • The <i class="osg-outcome__trend"> also receives some text?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.