This can be achieved with a single line that manipulates the DOM using the append
, filter
and hide
jQuery methods.
$('selector2').append(thingo)
– append the items.filter('selector1')
– of the original selector, only select those matching the filter.hide()
– hide the filtered items
Like this:
$(function()
{
var thingo = $('<div />');
$('selector2').append(thingo).filter('selector1').hide();
}
Optionally, if you want to hide the appended items, you’ll want to add an additional chain after the filter
, whereby you could use the find()
method, like this:
// this will hide all child divs, so you may want to be more specific
$('selector2').append(thingo).filter('selector1').find('div').hide();
1
solved jQuery : Hide select DOM elements created on the fly