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

228
Views
Make Selected part of text Bold/Unbold with JavaScript

When writing something in “input” it’s real-time visible in div. It’s necessary that when B button is pressed the selected text in div turns bold and then it becomes unbold when pressed again. But I don't need any library or framework. I need to use only JavaScript. I tried getSelection methods and execCommand but they did not help․

const textArea = document.getElementById("textArea");
const input = document.getElementById("input");
const bold = document.getElementById("bold");

let span = document.createElement("span");

input.addEventListener("input", e => {
  span.textContent = e.target.value;
  textArea.appendChild(span);
});
#textArea {
  width: 360px;
  height: 350px;
  padding: 5px;
  font-size: 15pt;
  word-wrap: break-word;
  overflow: scroll;
  border: 2px solid #ccc;
}

button {
  width: 150px;
  height: 50px;
  border: none;
  cursor: pointer;
}

.inputDiv {
  margin-top: 5px;
}

#input {
  width: 370px;
  height: 50px;
  border: none;
  outline: none;
  background-color: #ccc;
  font-size: 15pt;
}

#workspace button {
  width: 50px;
  margin: 5px auto;
  background-color: #d9d9d9;
}

#workspace button:hover {
  width: 50px;
  margin: 5px auto;
  background-color: #999999;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <div class="myDiv">
    <div id="workspace">
      <button id="bold">B</button>
      <div id="textArea"></div>
    </div>
    <div class="inputDiv">
      <input type="text" name="text" id="input" placeholder="type something...">
    </div>
  </div>
  <script src="script.js"></script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
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!