views:

2662

answers:

4

I have script that performs from DOM manipulation to change the way a div looks. The way the application is, when the page loads, the div is rendered in its original state, the function manipulates the DIV. The problem is it renders the original div before rendering the changed div.

I tried:

//Make div invisible

//Call function to change the DIV

//Make div visible

This does not work

A: 
$('div').hide(function(){
 $(this).css('font-size', '5em');
 $(this).find('h1').css('color','red');
 $(this).show();
})

This MUST work ;)

Ionut Staicu
Can't you chain that? $(this).css('font-size', '5em').find('h1').css('color', 'red').show();
RSolberg
@Solberg, Yes, you can. Slightly less readable, though.
strager
@Solberg: if you chain it like that the show() would only be on the h1, not the entire div.
Paolo Bergantino
i can chain, but i wanted to show how to do many stuff there :) Can be font size or can be content or anything else, more important is how is done ;)
Ionut Staicu
@paolo: $(this).css('font-size', '5em').find('h1').css('color', 'red').end().show(); will do the one-line trick :)
Ionut Staicu
+1  A: 

Are you using the visibility attribute?

<div id="divy" style="display:none;">Wee</div>

or set the style in a .css file

$(document).ready(function(){
   $("#divy").css("color", "#424242");
   $("#divy").show();
});
hunter
as already mentioned above in comments, never hide content by default
bandi
says who? If an html objects default state is hidden then this is necessary. THIS comment item is hidden by default!
hunter
@Hunter - That's because it relies on AJAX to retrieve comments and therefore is not enabled if JavaScript is disabled and makes logical sense in this scenario to hide by default. I did state in my comment that under certain circumstances it is bad practice
Russ Cam
but my original comment with that in has been deleted
Russ Cam
I didn't read anywhere in Bob Smith's post that he was considering browsers without javascript enabled. I just started using this StackOverflow and I didn't want to start making "what-if" statements. This was a jQuery question and I gave a jQuery answer. noscript could include another .css to fix.
hunter
The intention with my original comment was not to start an argument, merely to point out that I believe your answer would be more thorough if it highlighted that setting the display style to none may have consequences, depending on what the OP is doing in the div, for JavaScript disabled users...
Russ Cam
A: 

If you don't want it showing right away, you should just hide it with CSS (rather than JS). Then you run your change function and display it.

fig
A: 

Are you just trying to keep the div visible while preventing the changes from being seen? You could probably make it happen by cloning the div, making your changes on the cloned div, then replacing the original div with the cloned, changed div. Something like:

var changed_cloned_div = make_changes( $('#original_div').clone() );
$('#original_div').replaceWith( changed_cloned_div );
Parand