tags:

views:

61

answers:

4

I just got a script to work by changing

$('#thisElement').show();

to

$('#thisElement').css({'display':'block'});

where #thisElement had been loaded as having display:none;

Is there a fundamental difference in these two expressions?

A: 

If you had hidden #thisElement using $('#thisElement').hide();, and it had a display property that was not block, show() restores whatever the value was before you hid it.

BoltClock
Seriously? `show()` won't show a hidden element unless it was `hide()`-ed first (or has an inline style)? How did I not trip over this sooner?
Isaac Lubow
I'm saying that if you had set a different display style on it then it restores that value. It can show any hidden element.
BoltClock
@BoltClock: But it didn't. That was why I asked the question. Oddly, actually, I put an alert in, to see if the script was really executing. And with the alert present, `show()` _did_ execute. So the likely explanation is that `show()` takes longer to execute.
Isaac Lubow
+2  A: 

Assuming you have this at the beginning:

<span id="thisElement" style="display: none;">Foo</span>

when you call:

$('#thisElement').show();

you will get:

<span id="thisElement" style="">Foo</span>

while:

$('#thisElement').css({'display':'block'});

does:

<span id="thisElement" style="display: block;">Foo</span>

so, yes there's a difference.

Darin Dimitrov
That's not what it says [here](http://docs.jquery.com/Show).
Isaac Lubow
takteek
A: 

show() allows for other display values, like inline. From the documentation:

This is roughly equivalent to calling .css('display', 'block'), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

Bozho
+2  A: 

From the jQuery show() documentation:

This is roughly equivalent to calling .css('display', 'block'), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

takteek