how can I iterate through the divs inside one div, and if they all have the same ‘display’ value(‘none’), display a message?
I’m just learning and I need to use it one hundred percent
jQuery
<div class="test1">
<div class="test2" style="display:none">
</div>
<div class="test2" style="display:none">
</div>
<div class="test2" style="display:none">
</div>
<div class="test2" style="display:none">
</div>
</div>
I tried it using jQuery.each
jQuery('.test1 > .test2').each(function(){
if(jQuery(this).css('display') == 'none'){
jQuery('.test_message').text('nothing');
}
})
3
Answers
The issue with your current logic is that you are testing each
.test2
div individually, then outputting the message based on that single element. You instead need to check all the elements first, then output the message if necessary.To do that you can store a flag to determine the state of the divs, then change that flag if any one of them does not match that state. Here’s a working example:
It would also be possible to do this without an explicit loop by using the
:visible
selector:Finally, note that in the examples above I used a shorthand
document.ready
handler, which aliases the$
variable. This means you are safe to use the$
prefix instead of the verbosejQuery
keyword, without it affecting any other scripts which may also be using$
.The easiest way is to change your logic. You can select all elements that have not the
style="display:none"
attribute and check the length of the Node List with!NodeList.length
. If this statement is true, then all elements will have the attribute as nothing is returned:As side-note: As said you should not focus on 100% jQuery in 2024. even Bootstrap-5 got rid of jQuery 2 years ago as it makes no longer sense. It is "outdated" as plain Javascript caught up with the functionality.
If you want to select all child elements in JS you could also use
querySelector('element').children
Some of the other solutions will fail if the display value is set in external CSS
Instead use checkVisibility
Also note I use the more readable Array.from instead of the […collection] spread operator to get the array methods available on the collection
If you MUST use jQuery, then try