bonaca bonaca - 4 months ago 8
jQuery Question

hide all child elements on multiple divs

I have four divs and want to hide all child elements on each of them.

$('#col1').children().hide();
$('#col2').children().hide();
$('#col3').children().hide();
$('#col4').children().hide();


I believe there is a better way.

Answer

Yes, you can do all with only one line:

$('#col1, #col2, #col3, #col4').children().hide();

If you have only these divs you may reduce all to:

$('[id^="col"]').children().hide();

In this way all divs having an id starting with col will be selected.