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

  • SEARCH
  • Home
  • 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 7751345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:31:28+00:00 2026-06-01T11:31:28+00:00

I need some help with something… say I have the following form… <form name=

  • 0

I need some help with something… say I have the following form…

<form name="" id="" method="" action="">

    <input type="text" id="text1" name="text1" />
    <br />
    <br />

    <input type="text" id="text2" name="text2" />
    <br />
    <br />

    <input type="text" id="text3" name="text3" />
    <br />
    <br />

    <input type="text" id="text4" name="text4" />
    <br />
    <br />
    <input type="submit" value="let's go" disabled="disabled" />

</form>

Now I want to have a simple script to enable the submit when the values of the text boxes are not an empty string or null…

So I have something like this.. which I will bind to the window.onload

 function enableButton(){

    var formitemsArray = ['text1','text2','text3','text4'],
       i;

        // Loop through all items
        for(i=0;i<formitemsArray.length;i++){

            // validate the length on the keypress...
            formitemsArray.onkeypress = function(){

                // loop through all the items again
                for(j=0;j<formitemsArray.length;j++){

                    if(formitemsArray[j] == "" || formitemsArray[j] == null ){
                        // return false or something???
                    }else{
                        document.getElementById("submitButton").disabled = false;
                    }

                }
            }

        }

}

Now I think I’m on the right lines to a solution but I’m getting lost when trying to make sure that all the items are greater than a zero length string as I’m returning false too soon. Can someone set me straight please?

  • 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-06-01T11:31:29+00:00Added an answer on June 1, 2026 at 11:31 am

    Welcome to event bubbling!

    This does the following: listen to an event (onkeypress) on the whole element and all its children! Which means you can do the following:

    document.getElementById('form-id').onkeypress = function(e) {
        var text1 = document.getElementById('text1'),
            text2 = document.getElementById('text2'),
            text3 = document.getElementById('text3'),
            text4 = document.getElementById('text4')
    
        if (text1.value.length > 0 &&
            text2.value.length > 0 &&
            text3.value.length > 0 &&
            text4.value.length > 0) {
            document.getElementById('submit-button').disabled = false
        }
    
        // As an aside, for later: if you want to get the element
        // that triggered the event, you have to do the following
        // to be cross-browser:
        var evt = e || window.event, // IE doesn't get the event passed by argument
            target = e.target || e.srcElement // 'target' is official, old versions of FF used 'srcElement'
    
        // With the 'target' variable, you can now play.
    }
    

    There is another more generic solution, but it might not fit your needs (note that it requires a forEach shim:

    // Declare a counter variable
    var count = 0
    document.getElementById('form-id').onkeypress = function(e) {
        // Get all the inputs!
        var inputs = this.getElementsByTagName('input')
    
        // Now loop through all those inputs
        // Since a NodeList doesn't have the forEach method, let's borrow it from an array!
        [].forEach.call(inputs, loopThroughInputs)
    }
    function loopThroughInputs(input) {
        // First check the type of the input
        if (input.type === 'text') {
            // If the value is correct, increase the counter
            if (input.value.length > 0) {
                count++
            }
            // If the 4 inputs have increased the counter, it's alright!
            if (count === 4) {
                document.getElementById('submit-button').disabled = false
            }
        }
    }
    

    And now this code was proposed by @Esailija, and it is way better and cleaner. However, it also requires ES5-Shim (for the every method):

    document.getElementById('form-id').onkeypress = function(e) {
        var inputs = [].slice.call( this.querySelectorAll( '[type=text]') );
        document.getElementById('submit-button').disabled = !inputs.every(function(input){
            return !!input.value;
        });
    }
    

    (This guy is brillant, just don’t tell him)

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

Sidebar

Related Questions

Need some help with a query.. I have three tables. Source id name 1
I need some help with defining a dynamic method. Basically, I have many classes
I need some help on simplifying my method I have this method public double
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help with DataFormatString in GridView. I have a Double value that needs
I'm new to ORM stuff and I need some help understanding something. Let's assume
I need some help with something that is probably very basic. I'm working on

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.