Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Can i put css on a condition required in javascript?

I want to know if there is a way to add css on a required element in JavaScript I have many condition and i want it just in this case i want something like that (I know i can't do this) Thanks for your help !

  if (!allAreFilled) {                                          // While required element are empty
        alert('Fill all the fields');
        objForm.style:required.border = "solid 1px red";            // objForm = document.getElementById('compoundFormId')
      }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

With css:

input:required {
  border: 1px dashed red;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot directly change CSS style settings for pseudo elements or classes in Javascript.

But you can set CSS variables from JS.

This snippet sets a CSS variable --border when the submit button is clicked with the value depending on a condition.

const button = document.querySelector('button');
let conditionIsSet = true;

button.addEventListener('click', function() {
  document.body.style.setProperty('--border', (conditionIsSet) ? 'red' : 'black');
});
body {
  --border: black;
}

input:required {
  border: var(--border) 1px solid;
}
<input type="checkbox" onchange="conditionIsSet = !conditionIsSet;">Select condition is true</input>
<br> Input without required: <input> Input with required: <input required>
<button>Submit</button>

Obviously you need to supply whatever condition is needed.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda