I have a form with several fields that are required, and many that are not required. I want the required fields to be pale pink until they are clicked on and then revert to the default color of all of the fields. I haven't really tried anything because I am not sure how to formulate it. I created a different class for the fields with rgba color value. One example I found does addClass, but the fields I need to change already have a class to define their width, outline, etc. Would addClass CHANGE an existing class, OR is there a "changeClass" functionality or something like that? I tried to modify the answer here: Change Class value With Javascript to work when I clicked the field but that did not work. I tried using document.querySelectorAll too because I have multiple fields separated by other non-required fields and I do not want them to all have the same id or be in the same divs.
I tried
function changeClass(){
document.getElementByClass(".reqd").className = "ssmall4";
}
or
function changeClass(){
document.querySelectorAll(".reqd").className = "ssmall4";
}
with
<input onClick="changeClass()" type="number" id="certYear" name="certYear"value="2020" class ="reqd">
Can anyone connect the dots for me?
I can now get it to work on one field by using:
`<label for="certYear">Certification Year:
<br>
</label>
<input type="number" id="certYear" name="certYear"value="2020"
onclick="myFunction()" class="reqd">`
and
`function myFunction() {
document.getElementById('certYear').style.backgroundColor = "white";
}`
But if I change the function to document.getElementsByClassName I get "Uncaught TypeError: Cannot set property 'backgroundColor' of undefined" Same if I try to use document.querySelectorAll (I assume in this one it's because I have to define a variable and I do not know how to enact the bg style color change any way other than above)
I suppose I could just copy the function like 10 times, once for each field and just rename the function and change the id but this seems rather inelegant.