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

394
Vistas
How to format text create by a dropdown list?

In a dropdown list, I would like to format text from my list ?

In my example below for instance, I'd like to improve the presentation of "Variable 1 is built with ... (long text)" with bold, line breaks, bullets....

function getOption() {
  selectElement = document.querySelector('#select1');
  let selectedOption = '';
  output = selectElement.value;
  switch (output) {
    case "var1":
      selectedOption = "Variable 1 is built with ... (long text)";
      break;
    case "var2":
      selectedOption = "Variable 2 is important because... (long text)";
      break;
    default:
      selectedOption = "default selected";
  }
  document.querySelector('.output').textContent = selectedOption;
}
<p>
  <select id="select1">
    <option value="var1">Variable 1</option>
    <option value="var2">Variable 2</option>
    <option value="var3">Variable 3</option>
  </select>
</p>

<button onclick="getOption()"> Check option </button>

<p> The value of the option selected is:
  <span class="output"></span>
</p>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use innerHTML and style string directly like:

function getOption() {
  selectElement = document.querySelector('#select1');
  let selectedOption = '';
  output = selectElement.value;
  switch (output) {
    case "var1":
      selectedOption = "<br /><b>Variable 1</b> is built with<br />...<br />(long text)";
      break;
    case "var2":
      selectedOption = "<br /><b>Variable 2</b> is important because... (long text)";
      break;
    default:
      selectedOption = "<br /><b>default</b> selected";
  }
  document.querySelector('.output').innerHTML = selectedOption;
}
<p>
  <select id="select1">
    <option value="var1">Variable 1</option>
    <option value="var2">Variable 2</option>
    <option value="var3">Variable 3</option>
  </select>
</p>

<button onclick="getOption()"> Check option </button>

<p> The value of the option selected is:
  <span class="output"></span>
</p>

Reference:

  • Element.innerHTML
about 4 years ago · Juan Pablo Isaza Denunciar
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