1

I have a form with a lot of text fields and hidden input fields. Now I am trying to count those input fields of type text, but I can't get it to work.

I am using the following code:


    var subtagid = document.getElementById('subtags').getElementsByTagName('input').getAttribute("text").length;
var inputs = document.getElementById('subtags').getElementsByTagName('input').length;
for(var i=0; i<inputs.length; i++)
{
    if(inputs[i].getAttribute(‘type’)==‘text’)
    {
        subtagid++;
    }
}

Does anyone know how I can do this?

2
  • 1
    You should definitely not use weird quotes in code. And you only need var the first time. Commented Jan 30, 2020 at 21:58
  • You're looking for .getAttribute("text") instead of type but even that won't work since you're chaining that to a method that returns a list. Additionally, you're retrieving the length on inputs too soon, so you can't iterate over inputs[i]. Make it simple! document.querySelectorAll('input[type="text"]').length; (you can replace document with an ID selector if needed). Commented Jan 30, 2020 at 22:20

2 Answers 2

2

You should be able to count all the text elements by using querySelectorAll.

const count = document.querySelectorAll('#subtags input[type=text]').length;

Iterating over the elements is also possible.

for (let input of document.querySelectorAll('#subtags input[type=text]')) {
    // Do something with the input.
}

You can use any other valid CSS selector as well.

Sign up to request clarification or add additional context in comments.

2 Comments

would this count all the fields in the whole document or just in one form?
This will count all the text input fields inside of an element with the ID of "subtags".
1

You can't do this with a querySelectorAll because the default type of input is text. In order to get the correct number you have to check type of the inputs and either filter and get the length or iterate and add to subtagid:

text_inputs = inputs.forEach(input => input.type === "text" && subtagid++);

    var subtagid = 0,
    
    parent = document.querySelector("#subtags"),
    inputs = Array.from(parent.querySelectorAll("input")),
    text_inputs = inputs.forEach(input => input.type === "text" && subtagid++);

console.dir(subtagid);
<section id="subtags">
<input type="checkbox">
<input type="text">

<input type="button">
<input type ="text">
<input type="text">
<input>
</section>
<hr/>

<small> there are <strong>4</strong> Text Boxes </small>
<hr/>

<output>

</output>

3 Comments

@Addis uh. it's not wrong because that's literally what I said in my answer.
@Addis I don't mean to sound hostile here, but I believe you're wrong. The default input without a type defined in markup I believe isn't grabbed by a selector. Feel free to try it.
You made much sense now, though I don't think that's what the OP intended to do. Sorry for the misunderstanding, anyway.

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.