0

The following is the HTML that does what I want. It displays category: and beneath a value that I send in JavaScript.

<div id="category-order-id" class="additional-order-info">
   <!-- <span class="additional-order-info-grey">category:</span>
      Clothes -->
</div>

And here is my JavaScript

let orderCategory = document.getElementById("category-order-id");
orderCategory.textContent = `${order.category}`;
let categorySpan = document.createElement("span");
categorySpan.setAttribute("class", "additional-order-info-grey");
categorySpan.textContent = "category:";
orderCategory.appendChild(categorySpan);

What this code does, it that it first displays Clothes value that I send with JavaScript and beneath it display category.

Why I am creating a span element here is because if I set textContent of orderCategory it wil not display the span at all.

What am I doing wrong here?

1
  • 1
    It's not clear what your problem is here. Commented Oct 17, 2020 at 13:39

1 Answer 1

4

textContent property replaces all the content in the element. If you want to keep order.category and "category:" as content, you need to use the appendChild function:

let orderCategory = document.getElementById("category-order-id");
const catTextContent = document.createElement("span");
catTextContent.textContent = order.category;
let categorySpan = document.createElement("span");
categorySpan.setAttribute("class", "additional-order-info-grey");
categorySpan.textContent = "category:";
orderCategory.appendChild(catTextContent);
orderCategory.appendChild(categorySpan);
Sign up to request clarification or add additional context in comments.

Comments

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.