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

145
Vistas
How do you add/remove HTML ids/classes to elements using JavaScript?

I somewhat understand how to add elements with text to an HTML document using JavaScript. I'm just looking for a way to add an id/class to an element that is created from a function that's triggered by a event attribute in a form.

I want to display the following:


    
    #error {
    border: 0.5em red solid;
    }
    
    #correct {
    border: 0.5em green solid;
    }
    
<p id="error"> Must have at least one checkbox checked</p>
<p id="correct"> At least one checkbox is checked</p>

Please provide an example function that's triggered by an onsubmit event attribute utilized in a form element.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can add a class to an element using classList.add. Here is an example: HTML File

.errorStyle {
    border: 0.5em red solid;
}
    
.correctStyle {
    border: 0.5em green solid;
}
    
<p id="error"> Must have at least one checkbox checked</p>
<p id="correct"> At least one checkbox is checked</p>
<button onclick="change()">Submit</button>

JS Function

function change() {
  var element = document.getElementById("error");
  element.classList.add("errorStyle");
  element.classList.add("correctStyle");
}
about 4 years ago · Santiago Trujillo 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