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

232
Visualizações
Append text of different style

Hi is there a way to make the appended "YAY" be of a different style from the "I am happy" text? eg. bigger font-size/ bolded.

document.getElementById("appendButton").onclick = function() {

  document.getElementById("happy").innerHTML += " YAY";

}
<p id="happy">I am happy</p>
<button id="appendButton">Click here to cheer!</button>

I've tried to give it an id with span, but then the button won't append anything. Would appreciate any help thanks!

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

0

You won't be able to use an id because ids must be unique (which means you can't have more than one on a page and expect the correct output).

Add a class instead.

Note 1: I've used the more modern addEventListener method here.

Note 2: It's also worth mentioning that concatenating HTML strings to innerHTML is considered bad practice. insertAdjacentHTML is the better alternative.

const button = document.getElementById('appendButton');
const happy = document.getElementById('happy');

button.addEventListener('click', handleClick, false);

function handleClick() {
  happy.innerHTML += '<span class="yay"> YAY</span>';
}
.yay { color: blue; font-weight: 600; }
<p id="happy">I am happy</p>
<button id="appendButton">Click here to cheer!</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can append a span element with a class, then style it with CSS:

document.getElementById("appendButton").onclick = function() {
  document.getElementById("happy").innerHTML += "<span class='large'>YAY</span>";
}
.large{
  font-size:20px;
  font-weight:bold;
}
<p id="happy">I am happy</p>
<button id="appendButton">Click here to cheer!</button>

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