Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

0

With css:

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!