Thom
Thom

Reputation: 621

JavaScript: Count inputfields in a Div

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?

Upvotes: 1

Views: 12467

Answers (3)

Code Whisperer
Code Whisperer

Reputation: 23652

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

Upvotes: 0

Sean Quinn
Sean Quinn

Reputation: 2161

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.

Upvotes: 1

Brian Warshaw
Brian Warshaw

Reputation: 22984

var inputFormDiv = document.getElementById('inputForm');
alert(inputFormDiv.getElementsByTagName('input').length);

Upvotes: 8

Related Questions