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

273
Vistas
Change paragraph text using select (combo box) input using javascript

I'm trying to change all instances the word "blue" in a paragraph to another colour based on combo box input; the colour of the word changes to the corresponding input. Not sure what else to try. You can check my attached code.

document.querySelector(document).ready(function() {
  var button = document.querySelector("button_change");
  document.querySelector(button).click(function()

    {

      var select = document.querySelector("input_color").value;

      var element = document.querySelector(".cow_color");

      document.querySelector(element).html(inputValue);

    });
});
<h2>
  <span class="cow_color">Blue</span> Cow by Gelett Burgess (published in The Lark, 1895)
</h2>
<p>
  I never saw a <span class="cow_color">Blue</span> Cow, I never hope to see one; But I can tell you, anyhow, I'd rather see than be one.
</p>
<p>
  Change the cow's color to:
  <select id="input_color">
    <option value="Purple">Purple</option>
    <option value="Green">Green</option>
    <option value="Red">Red</option>
    <option value="Yellow">Yellow</option>
    <option value="Teal">Teal</option>
  </select>
  <button id="button_change">Change!</button>
</p>

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

0

  1. You either need an eventListener or an onclick-trigger to run the script.
  2. you use element.querySelectorAll('.class-name') as selector to select all classes with that class name
  3. to apply the changes to all elements you use the .forEach(el => el.command) command.

final JS line should be:

document.querySelectorAll('.cow_color').forEach(el => el.innerHTML = document.querySelectorAll('#input_color').value;);

function changeColor() {
  var inputValue = document.querySelector('#input_color'),
      cowColor = document.querySelectorAll('.cow_color');
  
  cowColor.forEach(el => el.innerHTML = inputValue.value);
}
<h2>
  <span class="cow_color">Blue</span> Cow by Gelett Burgess (published in The Lark, 1895)
</h2>
<p>
  I never saw a <span class="cow_color">Blue</span> Cow, I never hope to see one; But I can tell you, anyhow, I'd rather see than be one.
</p>
<p>
  Change the cow's color to:
  <select id="input_color">
    <option value="Purple">Purple</option>
    <option value="Green">Green</option>
    <option value="Red">Red</option>
    <option value="Yellow">Yellow</option>
    <option value="Teal">Teal</option>
  </select>
  <button id="button_change" onclick="changeColor()">Change!</button>
</p>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Overview

Heres a jquery version

Example

Example

Explination:

we bind to the button, then on click event, we get the value from the dropdown selection. with that value we loop through all .cow_color and assign the color with the value we got previously and also it replaces the Text.

Code

$(document).on('click', '#button_change', (element) => {
  let color = $('#input_color').val();
  let spans = $('.cow_color');
  spans.map(function() {
    $(this).css('color', color);
    $(this).html(color);
  });
  return;
});
<!-- Jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<!-- Body -->
<h2>
  <span class="cow_color">Blue</span> Cow by Gelett Burgess (published in The Lark, 1895)
</h2>
<p>
  I never saw a <span class="cow_color">Blue</span> Cow, I never hope to see one; But I can tell you, anyhow, I'd rather see than be one.
</p>
<p>
  Change the cow's color to:
  <select id="input_color">
    <option value="Purple">Purple</option>
    <option value="Green">Green</option>
    <option value="Red">Red</option>
    <option value="Yellow">Yellow</option>
    <option value="Teal">Teal</option>
  </select>
  <button id="button_change">Change!</button>
</p>

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