tags:

views:

30

answers:

4

I'm trying to hide some divs that I have created dynamically using the .hide() function but no luck. I think the reason it's not working is because elements that have not yet been added to the DOM. Is there a way around it?

$('<div class="toggle_container"></div>').html('<div
       class="block"><p>'+title+'</p></div>').appendTo('#page-wrap'); //dynamic creation of divs

$(".toggle_container").hide(); //trying to hide,but not working

Thanks

A: 

Use .live(), but as far as I know you have to use an event to do the actual hiding. When does the div have to be hidden?

Anyway, live() makes sure that elements that are created in the future are also included in a certain bind, whereas regular binds only work on elements that are in the DOM at the time of pageload.

$(".toggle_container").live('click', function() {
  $(this).hide();
});

Binds a click-event to the container as soon as it 'lives'. See here for reference.

Litso
Why would you need to use an event to do the hiding?
T.J. Crowder
Because `live()` requires one, or am I totally wrong?
Litso
@Litso the divs hav to be hidden as soon as the page loads,i could specify it in the CSS and it should work,but i was wondering if there is any way to could be hidden soon after the page loads via jquery.I was thinking of the .live function but how do i use it as soon as the page loads?
manraj82
@Litso: That's a bit of a circular argument (use `live`, but then you'll need an event). There's no need for either `live` or an event. He's added the divs to the DOM, he should be able to operate on them -- including making them hidden.
T.J. Crowder
I misunderstood the question. Thanks for correcting me :)
Litso
+1  A: 

Is page-wrap already in the DOM when you do this? Because if so, it seems to work: http://jsbin.com/ukite3 I've tried it with Chrome and Firefox on Linux, and IE8 and IE6 on Windows. That example uses your code above, then shows the div two seconds later (to prove that the addition worked). Works fine. If I comment out the part doing the hiding, I see the new content immediately.

If page-wrap isn't already in the DOM, you can't use selectors like $(".toggle_container") to update them, because selectors look through the DOM tree. (Your appendTo call should fail as well, since it will look in the DOM tree for an element with that ID.) If page-wrap isn't already in the DOM, you'd want to change your code like so:

var pageWrap = /* ...whatever creates the page-wrap element ... */;
var toggle = $('<div class="toggle_container"></div>');
toggle.html('<div class="block"><p>'+title+'</p></div>');
toggle.appendTo(pageWrap);
toggle.hide();

(You can condense that a little bit if you want to, chaining the last three lines.) Live example: http://jsbin.com/ukite3/2


Off-topic: I find that in situations like this, creating a self-contained, minimalist example can really help me find the actual underlying problem. I can't count the number of times I've been sure something was X, then isolated X out and found that no, it was Y all along... :-)

T.J. Crowder
@TJ pageWrap is already in the DOM,it should work like you said but something was affection the code,might have been the CSS classes i used.funny enough the following code worked $('<div class="toggle_container"></div>').hide().html('<div class="block"><p>'+title+'</p></div>').appendTo('#page-wrap'); Thanks for your answer mate
manraj82
@manraj82: No worries. In any case, immediately calling `hide` as suggested by Kristoffer works either way (or of course just put in a `style="display: none"` in the markup).
T.J. Crowder
A: 

It works fine http://jsfiddle.net/TMtCz/ You could however hide it before you append it, like so:

$('<div class="toggle_container"></div>').hide().html('<div class="block"><p>'+title+'</p></div>').appendTo('#page-wrap');
Kristoffer S Hansen
dude cheers that worked
manraj82
A: 

You can always use

document.getElementById('id').style.display = "none";

As this will look at all current DOM elements.

Tim B James
Please don't just flag someone down without a reason.
Tim B James