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

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

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 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!