0

Please note that this question already has an answer here but the answer is provided in jQuery, I need similar thing to be done with vanilla javascript.

Here's my issue in detail. I have a price string to display in a page which will be in html as <span>60.00</span> I want to transform the string into something like this <span>60,<sup>00</sup></span>.

Here's the code I have tried.

  let value = "60.00";
  value = value.replace(".", ",");
  let values = value.split(',');
  var tag = document.createElement("sup");
  var text = document.createTextNode(values[1]);
  text = tag.appendChild(text);
  let text1 = JSON.stringify(text);
  alert(values[0]+text1);

If I run this code I get 60{} as an output. 60,⁰⁰ this is the desired output.

1
  • 1
    Please don't modify your question in a way it invalidates valid answers. You should ask a new question. Commented Sep 16, 2021 at 7:12

3 Answers 3

2

You're almost there with your current code but you're concatenating objects and strings. That doesn't work. You have to fill the elements one by one or convert the HTML elements to strings with .outerHTML:

let value = "60.00";
value = value.replace(".", ",");
let values = value.split(',');
var tag = document.createElement("sup");
var text = document.createTextNode(values[1]);
tag.appendChild(text);
document.getElementById('output').innerHTML = values[0];
document.getElementById('output').appendChild(tag);
alert(values[0] + tag.outerHTML);
<div id="output"></div>

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

7 Comments

I can't use innerHTML I'm using angular
@Viira Why can't you use innerHTML with Angular?
I'm using pipe <span>{{foodItems.original_price | replace}}</span> I can't use pipes inside innerHTML
@Viira I edited my answer to create a HTML string. But you should know that Angular sanitizes HTML in strings. You can solve it with <span innerHTML="{{foodItems.original_price | replace}}"></span> or by using DomSanitizer
This solves my problem. Thanks
|
1

Instead of adding a sup tag, why not add the string <sup></sup> instead?

let span = document.querySelector("span");
let value = span.innerHTML;

const sup = `<sup>${value.split('.')[1]}</sup>`;
value = `${value.split('.')[0]},${sup}`;

span.innerHTML = value;
<span>60.00</span>

11 Comments

I have multiple span tags running on the page, it's a cart page. can we find the relative parent ?
I have tried your answer it works but it outputs as a string like this 60,<sup>00</sup>
you can give class to the spans you want to change
How can I add class names for elements that are generating dynamically from a foreach loop
Thank you for your effort. I appreciate it.
|
0

let span = document.querySelector("span");
let value = span.innerHTML;

const sup = `<sup>${value.split('.')[1]}</sup>`;
value = `${value.split('.')[0]}.${sup}`;

span.innerHTML = value;
<span>10.00</span>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.