In the following code:
$(document).ready( function () {
var hideInit = function () {
$(selector1).hide();
}
var loadInit = function () {
//get data
var thingo = $('<div />');
//populate thingo with a bunch of divs matching selector1
$(selector2).append(thingo);
}
loadInit();
hideInit();
});
I am parsing some data and populating the DOM with it in loadInit
, and then I wish to .hide
each of the elements present in the DOM that was just created which match selector1
.
Unfortunately the elements are not being hidden - what have I done wrong here?
Thanks!
Solution
My selectors weren't incorrect, as suggested by many, but it was the order in which I was calling the functions. In order to guarantee that hideInit
runs after loadInit
has complete, I call it at the end, inside, of loadInit
.
$(document).ready( function () {
var hideInit = function () {
$(selector1).hide();
}
var loadInit = function () {
//get data
var thingo = $('<div />');
//populate thingo with a bunch of divs matching selector1
$(selector2).append(thingo);
hideInit();
}
loadInit();
});
Thanks for your comments/ answers!
NOT related to : Using jQUery hide() on newly created dom elements
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…