javascript - How to calculate child div length from parent div; using classnames; in JQuery? -


the parent div have class name 'panel'.

it contains further child divs have class name 'clonablepart' , 1 button.

we need check

1) if there multiple clonablepart button default disable should turn enable

2) if there 1 clonable part button should remain disabled

note: class name 'panel' in parent div panel; can exist in child div's. 'input' can multiple

please see fiddle more details html structure. https://jsfiddle.net/k2rbs70m/

following jquery in use:

$('.myclass').each(function () {         var lengthofclones = $(this).closest('div.panel').find('.clonablepart').length;         var typeofclone = $(this).data("type");         console.log('length of clones:' + typeofclone + " - " + lengthofclones);          lengthofclones > 1 ? $('#delete' + typeofclone).attr('disabled', false) : $('#delete' + typeofclone).attr('disabled', true);     }); 

if unclear please write.

following html idea:

<div id="bank_panel" class="panel"> <div id="bank_panel1" class="clonablepart">     1) bank account </div> <div id="bank_panel1" class="clonablepart">     2) account name </div> <input type="button" value="delete" id="deletebank"  data-type="bank" disabled /> <i> make enable onload have 2 values</i> </div> 

if have 1 input child per panel class, can target this:

$(this).find('input') 

in code, give:

 lengthofclones > 1 ? $(this).find('input').attr('disabled', false) : $(this).find('input').attr('disabled', true); 

https://jsfiddle.net/ggngygzy/

edit

if have more 1 input child per panel, can find unique property , target it. example:

$(this).find('input[type=button]') 

Comments