Here's a simple way to move hidden list items:
$('#bob > li:hidden').appendTo('#cat');
This code snippet will transfer all hidden li
elements within the ul#bob
, including those with style="display:none;"
.
Elements can be considered hidden due to various reasons:
- They have a CSS property of
display: none;
.
- They are form elements marked as
type="hidden"
.
- Their width and height are explicitly set to 0.
- An ancestor element is hidden, causing the element to not be visible on the page.
Learn more about the hidden selector in jQuery documentation
If you specifically need elements with style="display: none;"
, you can use .filter()
along with a regular expression like this:
var pattern = /^\s*display:\s*none\s*;?\s*$/gi;
$('#bob > li').filter(function ()
{
return pattern.test($(this).prop('style'));
}).appendTo('#cat');
The regular expression provided is case- and whitespace-insensitive, allowing the semicolon to be optional, as per valid CSS syntax. If these details do not matter, a simpler one-liner approach can be used:
$('#bob > li[style="display:none;"]').appendTo('#cat');