Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda