0

I am using the following code to count the number of required input (textboxes) on an HTML5 form, which works fine.

var inputTags = document.getElementsByTagName('input');
reqinputCount = 0;
for (var i=0, length = inputTags.length; i<length; i++) {
    if (inputTags[i].validity.valid == false ) {
        reqinputCount++;
    }
}

Now my problem is that I also have a textarea on the form and want to include that as well. Can you please help me out. Thanking in advance.

3
  • How do you determine whether or not a tag is required? What is your validity attribute? Commented Mar 19, 2013 at 2:23
  • @Aiias Basic HTML5 form feature. Commented Mar 19, 2013 at 2:27
  • You can get a collection of every control in a form using the DOM 0 form.elements collection, then just iterate over that. Commented Mar 19, 2013 at 2:45

1 Answer 1

3

Since you're already relying on a feature only present in the newest versions of browsers, I'm assuming it's safe to assume you don't mind another such feature being used:

var inputTags = document.querySelector("input,textarea");
Sign up to request clarification or add additional context in comments.

1 Comment

If the OP wants to delve into HTML5 and not be backward compatible (not advised but might be fun for an exercise), he should read HTML5 §4.10 Forms. In any case, I think form.elements would be better to get all the controls, then deal with each as required (using HTML5 properties or stock HTML 4.01 as appropriate).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.