8

I have an element which has a CSS border and the problem is the element is empty until the contents are filled by jQuery, but the border is drawn by CSS from the outset. I would like to be able to do something like make the element visibility hidden until it's ready to display then show element and border in one go.

Is there some way to achieve this?

Edit: the code to display the contents are contained inside

$(window).load(function() {

}
byronyasgur
  • 4,627
  • 13
  • 52
  • 96

7 Answers7

14

Method 1

One method you could try would be setting the CSS of the element to display:none;. This would hide the element, and calling the following function would cause the element to display on DOM load.

$(document).ready(function () {
  // Put all of jQuery code in here
  $("element").show(); // substitute element for whatever is needed
});

However, I wouldn't suggest this method, because there will be a sudden jump of content once the jQuery executes, because display:none; means that there will be no trace of the element on the page

Method 2

The best method in my opinion would be to use visibility:hidden. This will cause the space where the element usually is to still appear, but there will be white-space in place of the element. That way, when the page loads, there isn't a sudden jump of content. If you use this method, you will need to use the .css() method, because .show() is essentially setting the CSS of the element to display:block; So the code will look like this

$(document).ready(function () {
  // Put all of jQuery code in here
  $("element").css("visibility", "visible") // substitute element for whatever is needed
});

For both of these methods, the CSS will make the element hidden, and this function waits until the entire DOM is loaded before it executes.

Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
  • +1 I like this, but if this was a one off element I'd probably just set inline `style='display:none'` instead of CSS visibility. – AaronLS Apr 15 '13 at 23:34
  • can i put the doc.ready stuff inside of window.load or should it be outside. really just wondering about keeping the code beside the original code because i have a good bit of other associated stuff in the same load block – byronyasgur Apr 15 '13 at 23:34
  • @AaronLS classes would be better to use rather than inline elements – Cody Guldner Apr 15 '13 at 23:35
  • @byronyasgur Should be outside. Just like window.load is just defining an event handler, document.ready is also defining an event handler. You want to make sure the doc.ready event handler is defined before doc.ready occurs, and therefore you shouldn't nest it inside another handler. – AaronLS Apr 15 '13 at 23:36
  • @byronyasgur You can put any code in the `document.ready` block that you would like – Cody Guldner Apr 15 '13 at 23:36
  • @AaronLS You can put what you want in the document block, but I agree. It would be better to define the `window.load` as a function, then call the function on the element in the `document` block – Cody Guldner Apr 15 '13 at 23:38
  • Actually for some reason document.ready didn't work for me and I ended up using a setTimeout too ... I think the reason for this is that I'm using a slider inside the element ( maybe should have mentioned that, sorry ) and it must wait for document.ready to draw stuff or something ... anyway I wouldn't have had a clue but for the answers here and I think this one is the best so awarding the tick. I did notice however that show() doesn't seem to work to show `visibility:hidden` - http://stackoverflow.com/questions/7204494/jquery-show-not-revealing-a-div-with-visibility-of-hidden – byronyasgur Apr 16 '13 at 00:00
  • Sorry to rant but just in case this is of any help to anyone else ... it eventually clicked with me that my slider ( Flexslider - which again sorry I hadn't mentioned ... I must have thought it was irrelevant ) has a method which fires when it's ready to show it's first slide; so I put my css('visibility', 'show') in in there instead of the timeout and it worked fine, and obviously better than setTimeout – byronyasgur Apr 16 '13 at 00:07
2

You could use jQuery addClass to add a style that gets the border, or perhaps removeClass to remove a class of hidden display:none;

And to answer your title question, it's document.ready that you're looking for!

Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
ArleyM
  • 855
  • 5
  • 15
2

Hide your item initially.

 $(function(){
    $('selector').hide(); // Or you can just have display:none
    });

In some other section some event fills in your div with some content

....
....//some code that fills in your content
...
$('selector').show();
....
....
PSL
  • 123,204
  • 21
  • 253
  • 243
1

Ready on elements will work:

$(document.getElementsByTagName('div')[0]).ready(function() {
// do stuff when div is ready
metamagikum
  • 1,307
  • 15
  • 19
0

You could use

$(function(){
  // your code here
});

So the above gets executed when the DOM is loaded. Now all you need to do is show your element which had visibility:hidden set in css.

griegs
  • 22,624
  • 33
  • 128
  • 205
  • When you use display:none, the element doesn't actually get loaded. This is why many people recommend visibility:hidden instead. display:none doesn't take up an dom space either, while visibility:hidden and opacity:0 both do. – Shazboticus S Shazbot Apr 15 '13 at 23:32
0

Try this:

$('#element').bind("DOMSubtreeModified", function () {
    //do stuff
});
Rafael A. M. S.
  • 637
  • 1
  • 6
  • 27
  • Depricated, see https://stackoverflow.com/questions/6659662/why-is-the-domsubtreemodified-event-deprecated-in-dom-level-3 – Lodewijk Aug 06 '15 at 17:03
-2

Have you tried .css()? You can change the css of an element. Read more here.

Initially, set it as hidden, then remove it.

Doon
  • 3,339
  • 5
  • 25
  • 31