最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find the number of <input> in an Div tag - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jul 28, 2009 at 9:20 praveenjayapalpraveenjayapal 38.5k31 gold badges74 silver badges74 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 13

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;

Very easy if you can use jQuery:

$('#divId input').length;

Otherwise @Jon Grant's answer is what you need

Take a look at the getElementsByTagName method.

Use jQuery:

$("div input").length
发布评论

评论列表(0)

  1. 暂无评论