praveenjayapal
praveenjayapal

Reputation: 38539

Find the number of <input> in an Div tag

I need to find out the number <input> tag within a <Div> tag.. How it's possible.. For example the code will be like this..

<div><li ><a>Jayan</a><input type="checkbox" id="c21" onClick="fcheck(this);" ></li>
<li ><a href="#">Reshaba</a><input type="checkbox" id="c22" onClick="fcheck(this);" >
    <ul>
        <li ><a>crescent</a><input type="checkbox" id="c221" onClick="fcheck(this);" ></li>
        <li ><a>crescent</a><input type="checkbox" id="c222" onClick="fcheck(this);" ></li>
        <li ><a>crescent</a><input type="checkbox" id="c223" onClick="fcheck(this);" ></li>
        <li ><a>crescent</a><input type="checkbox" id="c224" onClick="fcheck(this);" ></li>
    </ul>
</li></div>

Please help

Thanks,
Praveen J

Upvotes: 3

Views: 10260

Answers (4)

I.devries
I.devries

Reputation: 8916

Why always the jquery solutions ? There is nothing wrong with using jquery, but including a JS library to count some elements is serious overkill.

Native javascript:

var inputCount = document.getElementById('divId').getElementsByTagName('input').length;

Upvotes: 13

Keith
Keith

Reputation: 155692

Very easy if you can use jQuery:

$('#divId input').length;

Otherwise @Jon Grant's answer is what you need

Upvotes: 5

Svante Svenson
Svante Svenson

Reputation: 12478

Use jQuery:

$("div input").length

Upvotes: 1

Jon Grant
Jon Grant

Reputation: 11530

Take a look at the getElementsByTagName method.

Upvotes: 4

Related Questions