Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!