/    Sign up×
Community /Pin to ProfileBookmark

Hiding objects: visibility vs display, firefox vs safari

Hi all,

I am trying to modify the visibility of objects in Javascript and I am wondering if there is a way to use the same properties of the objects in all the browsers.

Here is a piece of code that has a weird behavior depending on the browser you use.

[CODE]
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<tr id=”tr1″>
<td>tr1</td>
</tr>
<tr id=”tr2″>
<td>tr2</td>
</tr>
<tr>
<td>
<input type=”button” value=”visibility=collapse tr1″
onClick=”document.getElementById(‘tr1′).style.visibility=’collapse’;” />
<input type=”button” value=”visibility=visible tr1″
onClick=”document.getElementById(‘tr1′).style.visibility=’visible’;” />
</td>
</tr>
<tr>
<td>
<input type=”button” value=”display=none tr1″
onClick=”document.getElementById(‘tr1′).style.display=’none’;” />
<input type=”button” value=”display=block tr1″
onClick=”document.getElementById(‘tr1′).style.display=’block’;” />
</td>
</tr>
</table>
</body>
</html>[/CODE]

You can test it on [URL=http://caractereschinois.free.fr/test.php]my website[/URL].

Here is what I would like to get:
– a button that collapses a row (the row below goes up);
– a button that makes it reappear.

Basically here is what I noticed:
Firefox 2.0:
– the two buttons visibility-collapse/visibility-visible do what I want;
– the display-block button adds a weird shift to the right;
– the two buttons block, none work well the first time and then they make new empty rows to appear, shifting the row tr2 to the bottom.

Safari:
– the visibility-collapse button does not collapse the row but makes it invisible;
– the display-block button adds the same weird shift to the right;
– the buttons display-block/display-none do what I would like (except from the slight shift).

I haven’t tried neither Konqueror nor IE but I am wondering if there is a browser-safe way to have this result without having to take care of the version of the browser.

Thanks a lot,

Figo

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 08.2007 — Only IE uses the value [I]block[/I] for table elements, others use table-row. See: http://www.w3.org/TR/REC-CSS2/visuren.html#display-prop

The trick is to reset to default, i.e. [I]document.getElementById('tr1').style.display='';[/I], which sets the display to [I]block[/I] for IE and [I]table-row[/I] for FF.

The visibility property is better used for making an element invisible, but still leaving height for the element in the document. Use [I]visibility:hidden[/I] for a more consistent result.
Copy linkTweet thisAlerts:
@figoauthorJan 09.2007 — Thanks, it is much better.
×

Success!

Help @figo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...