1

I am trying to update an HTML table row element with a text and icon. This is what I have tried so far:

const cell = document.getElementById("cellID");

if (true) { cell.innerHTML = {someText} {<i className="fa fa-exclamation-circle fa-fw" />} }

Here, cell is a cell in the the HTML table. For example, <td id="someId">some text</td>, I can update the text easily with cell.textContent = 'newText' but it does not work when I try to add an icon next to the text. It does not work. How can I add the icon with the text?

0

1 Answer 1

1

Have you tried with template literals

   if (true) { 
        cell.innerHTML = `${someText} <i class="fa fa-exclamation-circle fa-fw" />`
   }
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I just tried. It shows some text [object Object].
I got it to work. There is a slight change from what you recommended. if (true) { cell.innerHTML = ${someText} <i class="fa fa-exclamation-circle fa-fw" />` }`
I didn’t get the fix you did
if (true) { cell.innerHTML = '${someText} <i class="fa fa-exclamation-circle fa-fw" />' } I just used the icon element as a string in the innerHTML. Also, replaced 'className' with 'class'. Hope I explained it better now.
Yes. I thought of pointing the same. As we are directly manipulating them DOM element we have to change it to class instead of className to icon.

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.