admin管理员组

文章数量:1289602

I have a <div id="inputform"> and in that div there are multiple <input type="text"> . How can I count the number of <input> fields?

I have a <div id="inputform"> and in that div there are multiple <input type="text"> . How can I count the number of <input> fields?

Share Improve this question asked May 1, 2014 at 16:40 ThomThom 6215 gold badges14 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8
var inputFormDiv = document.getElementById('inputForm');
alert(inputFormDiv.getElementsByTagName('input').length);

Using jQuery you would be able to count the number of elements of a certain type, class, etc. using the following line of JavaScript

$("div#inputForm input").length

If you're not using jQuery, take a look at Brian's answer, it should do what you need it to.

$('#inputform input[type="text"]').length;

本文标签: htmlJavaScript Count inputfields in a DivStack Overflow