Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:18:26+00:00 2026-05-24T09:18:26+00:00

Anyone know of a good tutorial/method of using Javascript to, onSubmit, change the background

  • 0

Anyone know of a good tutorial/method of using Javascript to, onSubmit, change the background color of all empty fields with class="required" ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:18:28+00:00Added an answer on May 24, 2026 at 9:18 am

    Something like this should do the trick, but it’s difficult to know exactly what you’re looking for without you posting more details:

    document.getElementById("myForm").onsubmit = function() {
        var fields = this.getElementsByClassName("required"),
            sendForm = true;
        for(var i = 0; i < fields.length; i++) {
            if(!fields[i].value) {
                fields[i].style.backgroundColor = "#ff0000";
                sendForm = false;
            }
            else {
                //Else block added due to comments about returning colour to normal
                fields[i].style.backgroundColor = "#fff";
            }
        }
        if(!sendForm) {
            return false;
        }
    }
    

    This attaches a listener to the onsubmit event of the form with id “myForm”. It then gets all elements within that form with a class of “required” (note that getElementsByClassName is 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know any good tutorial about using NAnt for native code build process
Does anyone know about a good tutorial/article/whatever to help me explain JavaScript ( with
Does anyone know the location of a good list or tutorial showing all the
Does anyone know of a good tutorial on iPhone GUI design using just code
Anyone know of a good beginners tutorial for Turbogears 2? I'm particularly interested in
Does anyone know where I can find a good explanation/tutorial of what and how
Does anyone know of a good sample (inline in reply, or web tutorial) for
Does anyone know good example of using CSS to create rounded corner box where:
Does anyone know any good tutorial on how to upload a file with php
Just wondering if anyone know a good flex tutorial website besides Adobe. Appreciate the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.