Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
How do i change the color of an appended string?

I've a div

<div class="display-container"></div>

Inside this div i want to append some text using a JavaScript event listener

const calculatorDisplay = document.querySelector(".display-container")
function appendNumber(number) {
calculatorDisplay.append(number)
}
// number event listener
one.addEventListener("click", () => {
calculatorDisplay.append(1)
})

it work perfecly, but the problem here is that the background color of the display-container div is black, and the default color for string is black, so, how do i change the color of an appended string? i've already tried using the style tag, but that does not work, i've tried using fontcolor() too, but that too doesn't worked. I've noticed that the appended string have an id of #text, but i cannout use it if i try.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Define css class

<style>
.colored-text { 
   color: red;
}
</style>

And then create span element with colored-text class and append it

// number event listener
one.addEventListener("click", () => {
  const newSpan = document.createElement('span');
  newSpan.classList.add('colored-text');
  newSpan.textContent = 1;
  calculatorDisplay.append(newSpan);
})

BTW. why are you defining appendNumber function and not using it?

about 4 years ago · Juan Pablo Isaza Relatório

0

There are several ways to achieve this.

javascript

const calculatorDisplay = document.querySelector(".display-container")

// changing the color to red
calculatroDisplay.style.color = 'red';

// it accepts also Hex colors
calculatorDisplay.style.color = '#FF5733'

// OR rgb 
calculatorDisplay.style.color = 'rgb(255,0,0)

CSS

It is also possible to append a classname to your div. Like this you could make the code probably more reusable and may apply more styles than just colors in a simple manner. (There are multiple ways to include CSS in your html, google it^^ )

// within in the <head> tag in the html add a <style> tag. 
<html>
<head>
   <style>
       .red-color {
            color: red
        }
   </style>
</head>
<body>
<!-- .....  --->
</body>
</html>

In the code you can now add a classname using element.classList.add() OR element.classList.remove() to remove classes!

function setRedColor(el) {
    el.classList.add('red-color')
}

function removeRedColor(el) {
   el.classList.remove('red-color')
}

const calculatorDisplay = document.querySelector(".display-container")
setRedColor(calculatorDisplay)
// ...
removeRedColor(calculatorDisplay)

Note that the element.classList API generally does not allow classnames with a whitespace in it. So if you have mutliple classes you have to apply them one by one or you'll run into an error.

Feel free to leave a comment

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda