Anyone know of a good tutorial/method of using Javascript to, onSubmit, change the background color of all empty fields with class="required" ?
Anyone know of a good tutorial/method of using Javascript to, onSubmit, change the background
Share
Something like this should do the trick, but it’s difficult to know exactly what you’re looking for without you posting more details:
This attaches a listener to the
onsubmitevent of the form withid“myForm”. It then gets all elements within that form with a class of “required” (note thatgetElementsByClassNameis not supported in older versions of IE, so you may want to look into alternatives there), loops through that collection, checks the value of each, and changes the background colour if it finds any empty ones. If there are any empty ones, it prevents the form from being submitted.Here’s a working example.