markray markray - 5 months ago 17
jQuery Question

Get the Count of Input Tags inside a Div Element using the Closest Property Jquery

I know it is a stupid question but i couldn't find a solution for this. Knocking my head for hours.

I have a HTML Structure,

<div class= 'container'>
<div class="someclass">
<input>some content</input>
<input>some content</input>
</div>
<input id="question-xxx" type="hidden"></input>
</div>


I need to get the count of all the Input elements inside the class = 'someclass' using the id = "question-xxx".

I tried using

$("#question-xxx").closest('.someclass').find('input').length;


or

$('#question-xxx').closest('.someclass').children().length;


I googled it out, and I have no clue what I'm doing wrong. Any help would be quite appreciable.

Answer

.someclass will not be found using closest try finding it by siblings.

closest is used to find the parent element of given element. Here someclass is not parent of question-xxx.

closest and parent are same. You can find more info here

$("#question-xxx").siblings('.someclass').find('input').length